JavaScript - - By Sam Deering

jQuery Get html Including Element Tag

Incase you are using a lot of ajax functionality (I know I am) it might be useful to grab an elements html including the tag. I’m sure there must be an easier way than just cloning the element in a wrapper and then grabbing the wrapper html using jQuery to get the inner html including element tag html.

Until I find a better (more efficient way) here is the code snippet.

.clone().wrap('
').parent().html();

Some other ways (from post comments, thanks guys):

//Not sure how portable it is across browsers
$(‘#foo’)[0].outerHTML;
var foo = $(‘#bar’);
var foo = $('h1');

console.log(foo);
//output: jQuery(h1) < - DOM Element

console.dir(foo[0]);
//output: DOM element props and funcs

console.log(foo[0]['outerHTML']);
//output: 

Title

Sponsors