🤯 50% Off! 700+ courses, assessments, and books

jQuery Get html Including Element Tag

Sam Deering

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.


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

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

//output: jQuery(h1) < - DOM Element

//output: DOM element props and funcs