Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Populate content inside a new element before appending it

I'm trying to populate featured with data then append it to #wrapper. This isn't working for me. What am i doing wrong?

var featured = '<div id="featured"></div>';
$('#imageTable tbody tr td img').each(function(){
    $(featured).append($(this).attr('src'));
});
$(featured).appendTo('#wrapper');
like image 681
Pinkie Avatar asked Dec 04 '25 14:12

Pinkie


1 Answers

var $featured = $('<div id="featured"></div>');
$('#imageTable tbody tr td img').each(function(){
    $featured.append($(this).attr('src')).append('<br />');
});
$featured.appendTo('#wrapper');

Explaination:

When you do this inside the loop $(featured), it creates a NEW div for each selected element, so only the last div will be appended to your #wrapper. You need to create the jQuery object outside of the each loop.

like image 89
Samuel Liew Avatar answered Dec 07 '25 04:12

Samuel Liew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!