118

Imagine I have the following HTML:

<div><span><b>This is in bold</b></span></div>

I want to get the HTML for the div, including the div itself. Element.innerHTML only returns:

<span>...</span>

Any ideas? Thanks

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Richard H
  • 38,037
  • 37
  • 111
  • 138

10 Answers10

115

Use outerHTML:

var el = document.getElementById( 'foo' );
alert( el.outerHTML );
Majkel
  • 2,183
  • 1
  • 18
  • 13
94

Expanding on jldupont's answer, you could create a wrapping element on the fly:

var target = document.getElementById('myElement');
var wrap = document.createElement('div');
wrap.appendChild(target.cloneNode(true));
alert(wrap.innerHTML);

I am cloning the element to avoid having to remove and reinsert the element in the actual document. This might be expensive if the element you wish to print has a very large tree below it, though.

Jørn Schou-Rode
  • 37,718
  • 15
  • 88
  • 122
  • hi - on balance this is probably my best option. a little wary of detaching/attaching elements, I can't screw up the DOM, and I do not expect there to be large trees. – Richard H Nov 19 '09 at 14:29
  • Unless *myElement* is an element that can't be a child of a div, like an li, table row or table cell and so on. – RobG Jun 22 '11 at 04:23
  • 21
    Now that it's 2013, calling "domnode.outerHTML" works on all major browsers (FF since v11) – Kevin Oct 31 '13 at 17:23
10

First, put on element that wraps the div in question, put an id attribute on the element and then use getElementById on it: once you've got the lement, just do 'e.innerHTML` to retrieve the HTML.

<div><span><b>This is in bold</b></span></div>

=> <div id="wrap"><div><span><b>This is in bold</b></span></div></div>

and then:

var e=document.getElementById("wrap");
var content=e.innerHTML;

Note that outerHTML is not cross-browser compatible.

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • 2
    the problem with getting the innerHTML for the div's parent is that I'll also get the innerHTML for the div's siblings – Richard H Nov 19 '09 at 14:07
  • My bad, I thought the "put an id attribute on the element" referred to "the element". With your last edit it is a lot clearer that you want to add an additional `div` to the soup :) – Jørn Schou-Rode Nov 19 '09 at 14:09
  • @J-P: if the element is not in the `document`... then where is it, do you care to enlighten us all? – jldupont Nov 19 '09 at 14:21
  • @J-P: of course you can create elements where you want but this is **no** reason for a down-vote... you won't make much friends around here with that sort of attitude. – jldupont Nov 19 '09 at 14:25
  • Pretty sure outerHTML has been cross-browser compatible for a long, long time - don't be afraid of using it. – Snow Mar 14 '19 at 00:38
6

old question but for newcomers that come around :

document.querySelector('div').outerHTML

pery mimon
  • 7,713
  • 6
  • 52
  • 57
3

You'll want something like this for it to be cross browser.

function OuterHTML(element) {
    var container = document.createElement("div");
    container.appendChild(element.cloneNode(true));

    return container.innerHTML;
}
Nikolas Stephan
  • 1,280
  • 12
  • 10
3

If you want a lighter footprint, but a longer script, get the elements innerHTML and only create and clone the empty parent-

function getHTML(who,lines){
    if(!who || !who.tagName) return '';

    var txt, ax, str, el= document.createElement('div');
    el.appendChild(who.cloneNode(false));
    txt= el.innerHTML;
    ax= txt.indexOf('>')+1;
    str= txt.substring(0, ax)+who.innerHTML+ txt.substring(ax);

    el= null;
    return lines? str.replace(/> *</g,'>\n<'): str;
    //easier to read if elements are separated
}
kennebec
  • 102,654
  • 32
  • 106
  • 127
2

as outerHTML is IE only, use this function:

function getOuterHtml(node) {
    var parent = node.parentNode;
    var element = document.createElement(parent.tagName);
    element.appendChild(node);
    var html = element.innerHTML;
    parent.appendChild(node);
    return html;
}

creates a bogus empty element of the type parent and uses innerHTML on it and then reattaches the element back into the normal dom

Zenon
  • 1,436
  • 1
  • 11
  • 21
2
var x = $('#container').get(0).outerHTML;
Randy
  • 9,419
  • 5
  • 39
  • 56
0

define function outerHTML based on support for element.outerHTML:

var temp_container = document.createElement("div"); // empty div not added to DOM
if (temp_container.outerHTML){
    var outerHTML = function(el){return el.outerHTML||el.nodeValue} // e.g. textnodes do not have outerHTML
  } else { // when .outerHTML is not supported
    var outerHTML = function(el){
      var clone = el.cloneNode(true);
      temp_container.appendChild(clone);
      outerhtml = temp_container.innerHTML;
      temp_container.removeChild(clone);
      return outerhtml;
    };
  };
Remi
  • 20,619
  • 8
  • 57
  • 41
-2
var el = document.getElementById('foo');
el.parentNode.innerHTML;
Jason Leveille
  • 1,190
  • 5
  • 10