Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the string representation out of a jQuery object?

Tags:

jquery

Really dumb question but, for instance, given:

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

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

like image 625
longda Avatar asked Aug 17 '10 23:08

longda


2 Answers

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();
like image 109
user113716 Avatar answered Oct 09 '22 07:10

user113716


$("<h1>Test</h1>")[0].outerHTML

Returns: <h1>Test</h1>

like image 35
Sparklellama Avatar answered Oct 09 '22 07:10

Sparklellama