0

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?

3 Answers 3

1

Try this:

$(function(){
  $("a.link").each(function(){
    $(this).clone(true).appendTo($(this).parent().siblings(".item-description"));
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is what he want... copy the corresponding .link to its .item-description
BINGO! ... That did it! Thanks a million!
1
$(function(){
    $(".item").each(function(){
        $(this).find(".item-description")
            .append($(this).find("a.link").clone(true));
    });
});

1 Comment

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

2 Comments

This won't work since it will clone the first a and then append it to every .item-description div.
That copies the FIRST items link to all the subsequent items. I need to copy each items link to that particular items description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.