Skip to content

Instantly share code, notes, and snippets.

@dorey
Created September 13, 2012 16:12
Show Gist options
  • Save dorey/3715432 to your computer and use it in GitHub Desktop.
Save dorey/3715432 to your computer and use it in GitHub Desktop.
possible jquery extension outerHTML and toString methods
$.fn.outerHtml = function() {
// edit:
// var el = $(this).get(0);
var el = this.get(0);
if (el === undefined || !(el instanceof Element)) return false;
if (el.outerHTML !== undefined) return el.outerHTML;
return $('<div>').html($(this).eq(0).clone()).html();
};
/* It could be convenient if jQuery objects had a toString method
* defined that returned the outerHTML of the element rather than
* '[object Object]'.
*/
$.fn._original_toString = $.fn.toString;
$.fn.toString = function() {
if (this.get(0) instanceof Element) {
return this.outerHtml();
} else {
return this._original_toString();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment