Yerpasaur
Yerpasaur

Reputation: 71

Jquery: Clone and append items

How do you write a loop to append the rest of the items?

<div id="first">
<li><span>john</span></li>
<li><span>jane</span></li>
</div>

<div id="last">
<li><span>smith</span></li>
<li><span>doe</span></li>
</div>

$("#last span:first).clone().appendTo("#first li");

Upvotes: 0

Views: 51

Answers (3)

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

newelement = element.cloneNode(bool)

see this exapmle ..may be it will help you

Upvotes: 0

Jeff Koch
Jeff Koch

Reputation: 366

If you mean all of the items in the last list, you don't need to loop. just remove the :first filter from your selector like so:

$("#last span").clone().appendTo("#first li");

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816262

With .each and using the index of the element:

var $target = $('#first li');
$('#last span').each(function(i) {
    $(this).clone().appendTo($target.eq(i));
});

Upvotes: 2

Related Questions