JQuery stop add div delete

I am copying a div to another div using append. However, it removes the original. I tried to use a clone but it only seems to work with appendTo. But appendTo is breaking my layout so I need to use append with fine works.

I am wrong that the clone won't work with .append and is there any other way to stop the div from being removed?

thank

              $('.compareWrapper').append(htmlStr)                              

      

+3


source to share


3 answers


foo.appendTo(bar)


Take foo

and add it to bar

.

foo.append(bar)


Take bar

and add it tofoo

Syntactically they are different. You have to think about which target and which target. So, having said that you can move forward in one of two ways:



var $clone = $('target').clone();
$clone.appendTo('wrapper');
$('wrapper').append($clone);

      

Both do the same.

+4


source


Does not work?



$('.compareWrapper').append($(htmlStr).clone());

      

+1


source


I see no reason .clone()

not to work with .append()

. The code should be:

$('.compareWrapper').append($(htmlStr).clone());

      

Is this what you tried? From the name of your variable, I am assuming that htmlStr

is a string and not a jQuery object.

+1


source







All Articles