public
Last active

$.fn.outerHTML method

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
* Full example here: http://jsfiddle.net/jboesch26/3SKsL/1/
*/
 
$.fn.outerHTML = function(){
// IE, Chrome & Safari will comply with the non-standard outerHTML, all others (FF) will have a fall-back for cloning
return (!this.length) ? this : (this[0].outerHTML || (
function(el){
var div = document.createElement('div');
div.appendChild( el.cloneNode(true) );
var contents = div.innerHTML;
div = null;
return contents;
})(this[0]));
 
}
 
console.log('native outerHTML: ' + $('#colors')[0].outerHTML);
console.log('jQuery cross-browser outerHTML: '+ $('#colors').outerHTML());

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.