KulerGary
KulerGary

Reputation: 217

jQuery Clone Problems

Here is my scenario:

<div class="item">
<div class="item-title">
<a href="_files/download_item.zip" class="link">Download 1</a>
</div>
<div class="item-description"> Lorem ipsum dolor…. </div>
</div>

What I want to do is to clone the anchor and append it to the "item-description" div.

The problem is when I have multiple items that the clone function then copies ALL of the anchors to ALL of the "item-description" divs.

Here is the jquery I am using:

$(function(){   
    $('a.link').clone(true).appendTo('.item-description');
}); 

Can anyone spot what I am doing wrong?

Upvotes: 0

Views: 622

Answers (3)

thecodeparadox
thecodeparadox

Reputation: 87073

$(function(){   
    var links = $('.item-title a.link');
    links.each(function() {
       $(this).parent().next('.item-description').append($(this).clone(true));
    });
});

Upvotes: 1

hunter
hunter

Reputation: 63562

$(function(){
    $(".item").each(function(){
        $(this).find(".item-description")
            .append($(this).find("a.link").clone(true));
    });
});

Upvotes: 1

robbrit
robbrit

Reputation: 17960

Try this:

$(function(){
  $("a.link").each(function(){
    $(this).clone(true).appendTo($(this).parent().siblings(".item-description"));
  });
});

Upvotes: 1

Related Questions