Skip to content

Instantly share code, notes, and snippets.

@jbasdf
Created February 8, 2011 22:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbasdf/817477 to your computer and use it in GitHub Desktop.
Save jbasdf/817477 to your computer and use it in GitHub Desktop.
An outerHtml method for jQuery
jQuery.fn.outerHtml = function(include_scripts) {
if(include_scripts === undefined){ include_scripts = false; }
var clone = this.clone();
var items = jQuery.map(clone, function(element){
if(jQuery.nodeName(element, "script")){
if(include_scripts){
var attributes;
if(element.attributes){
attributes = jQuery.map(element.attributes, function(attribute){
return attribute.name + '="' + attribute.value + '" ';
});
}
return '<' + element.nodeName + ' ' + attributes.join(' ') + ">" + jQuery(element).html() + "</" + element.nodeName +'>';
} else {
return '';
}
} else {
return jQuery('<div>').append(element).remove().html();
}
});
return items.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment