By Sam Deering

jQuery Get html Including Element Tag

By 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) Title
  • There is a faster way: $(‘#foo’)[0].outerHTML

    Not sure how portable it is across browsers though…

  • IdemIsDito

    var foo = $(‘#bar’);

    the foo variable will be the element including the outer tag element.

    • jquery4u

      @IdemlsDito, thanks, this seems to work better?

      var foo = $(‘h1’);

      //output: jQuery(h1) < - DOM Element console.dir(foo[0]); //output: DOM element props and funcs console.log(foo[0]['outerHTML']); //output:


    • starrychloe

      Yes, but it’s not a String! You cannot use String functions on it!

Get the latest in JavaScript, once a week, for free.