Search code examples
jqueryclone

jQuery - clone() is not a function


I'm trying to copy some of my elements from this block and paste them in the end:

<div class="brands">
    <img src="brand1.png" alt="1" />
    <img src="brand2.png" alt="2" />
    <img src="brand3.png" alt="3" />
</div>

That's what I'm doing:

for (var i = 0; i < 2; i++) {           
    $('.brands img')[i].clone().appendTo('.brands');            
}

But then I see this in console: TypeError: $(...)[i].clone is not a function, jQuery is uncluded. What's wrong with my code?


Solution

  • You don't need the loop. https://api.jquery.com/slice/

    $('.brands img').slice(0, 2).clone().appendTo('.brands');
    

    The issue you were having is that once you extract the native Element by using [index] or .get(index) out of the $() function you loose jQuery Object methods chainability.