understanding effect of jquery statement

I was looking for an easy way to get the contents of a jquery element and came across this page: http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html

It has the following code

var html = $  ('<div>').append($  ('#top').clone()).remove().html();

Their explanation is as follows:

1) Cloning selected div

2) Creating a new div DOM object and appending created clone of the div

3) Then getting the contents of wrapped div (which would give us all HTML)

4) Finally removing the created DOM object, so it does not clutter our DOM tree.

My question are as follows:
1) it looks like the first thing this does is create a new div that is not attached to the dom, so why does it need to have the “remove” call?
2) I understand why it doesn’t come up for this example, but in general is there much difference between doing

A:  $  ('<div>').append('<span>foobar</span>')
B:  $  ('<div><span>foobar</span></div>)

newest questions tagged jquery – Stack Overflow

About Admin