tags:

views:

32

answers:

2

Really dumb question but, for instance, given:

var $foo = $('<div>bar</div>');

How would I get the '<div>bar</div>' back out?

A: 
Pointy
nope, this just returns "bar"
longda
`$otherFoo` is referencing the cloned `$foo`. You would need to traverse up to its parent (the wrapper `<div>`) in order to get its `.html()`.
patrick dw
oh right; sorry @patrick I make that mistake all the time in actual code!! Fixing post now.
Pointy
+2  A: 

You need to append it to a container, then call .html() on that.

This is because .html() only gives you the content.

Try it out: http://jsfiddle.net/ttYXG/

var $foo = $('<div>bar</div>');

$('<div />').append($foo).html();
patrick dw
Yup, that's it!
longda
@longda - If needed, you can get rid of the wrapper `<div>` with `$foo.unwrap();`. May not really be necessary since `$foo` still references your original element.
patrick dw