Skip to content

Instantly share code, notes, and snippets.

@seamusjr
Created January 6, 2011 17:33
Show Gist options
  • Save seamusjr/768231 to your computer and use it in GitHub Desktop.
Save seamusjr/768231 to your computer and use it in GitHub Desktop.
From http://blog.stevenlevithan.com/archives/faster-than-innerhtml using innerHTML and dom methods to replace elements
function replaceHtml(el, html) {
var oldEl = typeof el === "string" ? document.getElementById(el) : el;
/* @cc_on // Pure innerHTML is slightly faster in IE
oldEl.innerHTML = html;
return oldEl;
@*/
var newEl = oldEl.cloneNode(false);
newEl.innerHTML = html;
oldEl.parentNode.replaceChild(newEl, oldEl);
// Since we just removed the old element from the DOM, return a reference
// to the new element, which can be used to restore variable references.
return newEl;
};
// Example, replace #foo element with 2nd arg.
//replaceHtml('foo','<div id="bar">Bar</div>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment