Lukas
Lukas

Reputation: 7734

jQuery How to clone() more than one time

i'v clone some elements list, but i need to clone it more than one time, can you tell me how to do it?

$('.main_content ul li').clone().prependTo('.main_content ul')

Much thx for help.

Upvotes: 1

Views: 3716

Answers (3)

user2793503
user2793503

Reputation: 9

Or instead of using .prependTo() replace that with .insertAfter()

for example:

jQuery

$(function() {
    for () {
        $('.main_content ul li').clone().insertAfter('.main_content ul li');
    }
});

JsFiddle

Upvotes: 0

Christopher
Christopher

Reputation: 431

I'm not entirely sure what you are asking, you want to clone listed tags into it's same parent? This will result in the same listed items being in the same parent.

Or do you want to clone the parent? Or do you want to clone only certain listed items?

Is this what you are looking for?

<div class="main_content">
    <ul>
        <li>1</li>
        <li>2</li>
    </ul>
</div>

    <script type="text/javascript">

    $(document).ready(function(){
        var numberOfCopies = 5;

        for(x = 0; x < numberOfCopies; x++){
            $(".main_content ul li").each(function(){
                $(this).clone().appendTo(".main_content ul");
            });
        }
    })

</script>

Upvotes: 0

Vladislav Qulin
Vladislav Qulin

Reputation: 1942

Try this maybe?

var objToClone = $('.main_content ul li');
for (var i = 0; i < 10; i++)
   objToClone.clone().prependTo('.main_content ul');

Upvotes: 2

Related Questions