How to get the HTML for a DOM element in javascript

JavascriptHtmlDom

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

Use outerHTML:

var el = document.getElementById( 'foo' );
alert( el.outerHTML );

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

old question but for newcomers that come around :

document.querySelector('div').outerHTML

Solution 5 - Javascript

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;
}

Solution 6 - Javascript

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
}

Solution 7 - Javascript

var x = $('#container').get(0).outerHTML;

Solution 8 - Javascript

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

Solution 9 - Javascript

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;
    };
  };

Solution 10 - Javascript

var el = document.getElementById('foo');
el.parentNode.innerHTML;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichard HView Question on Stackoverflow
Solution 1 - JavascriptMajkelView Answer on Stackoverflow
Solution 2 - JavascriptJørn Schou-RodeView Answer on Stackoverflow
Solution 3 - JavascriptjldupontView Answer on Stackoverflow
Solution 4 - Javascriptpery mimonView Answer on Stackoverflow
Solution 5 - JavascriptNikolas StephanView Answer on Stackoverflow
Solution 6 - JavascriptkennebecView Answer on Stackoverflow
Solution 7 - JavascriptPawan JasoriaView Answer on Stackoverflow
Solution 8 - JavascriptZenonView Answer on Stackoverflow
Solution 9 - JavascriptRemiView Answer on Stackoverflow
Solution 10 - JavascriptJason LeveilleView Answer on Stackoverflow