September 15th, 2008
// add the outerHTML setter/getter to browsers that don't support it
if(!document.documentElement.outerHTML) {
HTMLElement.prototype.__defineGetter__('outerHTML', function() {
var wrapper = document.createElement(this.parentNode.tagName);
wrapper.appendChild(this.cloneNode(true)); // thanks Cristi
return wrapper.innerHTML;
});
HTMLElement.prototype.__defineSetter__('outerHTML', function(value) {
Element.replace(this, value); // requires prototype (http://prototypejs.org)
});
}
2 Responses to OuterHTML for the Masses
For the getter, you should use: wrapper.appendChild(this.cloneNode(true)); so your element is not removed after you get the outerHTML.
Good eye, Cristi. I'm not sure how I missed that. I've probably just been using this in places where it didn't matter. Or maybe the element wasn't being removed because wrapper isn't part of the document? Either way, I added in your suggestion. Thanks!
Leave a Comment