MShack
MShack

Reputation: 652

How to use clone and append and return the first instance of the img

I'm using the clone append to return a image to a new div , but the div i am cloning has several instances of the same image , and i only want to retrieve 1 copy. Can you use a selector in the following , like find the 1st instance of the image only ?

$( document ).ready(function() {
    $("#divOLD").find("img#franchiseicon_0000").clone().appendTo('#divNEW');
});

Upvotes: 0

Views: 71

Answers (1)

Amin Jafari
Amin Jafari

Reputation: 7207

this clones the first img in the #divOLD and appends it to #divNEW if that is in fact what you meant to say:

$( document ).ready(function() {
    $("#divOLD img").eq(0).clone().appendTo('#divNEW');
});

if you need to do it with a selector you can try:

$( document ).ready(function() {
    $("#divOLD img:first").clone().appendTo('#divNEW');
});

or

$( document ).ready(function() {
    $("#divOLD img:eq(0)").clone().appendTo('#divNEW');
});

Upvotes: 1

Related Questions