The problem with .html() methods is that it converts DOM elements to HTML and vice versa. You will lose all event handlers and data associated with these elements.
Use .contents() [docs] :
$("#div1").contents().appendTo('#div2').end().fadeOut();
or if you want to copy the contents:
$("#div1").clone(true, true).contents().appendTo('#div2'); $("#div1").fadeOut();
It is always useful to read documentation when using methods. For instance.
$('#div1').append('#div2');
will add the line #div2 to the element with the identifier div1 . The documentation states:
Insert the content specified by the parameter at the end of each element in the set of matched elements.
and
content : a DOM element, an HTML string, or a jQuery object to insert at the end of each element in a set of matched elements.
Similarly for $('#div1').html('#div2'); :
htmlString . An HTML string to set as the content of each matched element.
It will simply replace the contents of #div1 with the string '#div2' .
On the other hand
$('#div1').appendTo('#div2');
will add an element with identifier div1 to an element with identifier div2 :
Insert each element into the set of matched elements at the end of the target.
The jQuery documentation is pretty good, I recommend reading it.
source share