user1915308
user1915308

Reputation: 364

Jquery clone div issue

I want to clone a div one at a time when i click on a header

$("h3").click(function() { $("div").clone().insertAfter("div:last")); });

Right now it will clone all the divs and put them after the last one but i want to clone div1 then div2 etc.

Upvotes: 1

Views: 179

Answers (2)

algorhythm
algorhythm

Reputation: 8728

you can use .each() method to get the correct requirement.

$("h3").click(function() { 
    $("div").each(function() {
        $this = jQuery(this);
        // do something with current div $this
        $this.clone().insertAfter("div:last"));

        // a bit better?
        $this.parent().prepend($this.clone());
    });
});

i hope this help you more.

Upvotes: 2

ATOzTOA
ATOzTOA

Reputation: 36000

Try each:

$("h3").click(function() { 
    $.each($("div"), function() {
        $(this).clone().insertAfter("div:last"); 
    });
});

Upvotes: 1

Related Questions