Monday, May 2, 2011

Getting outerHtml with jQuery

From Volomike and jessica's answers on http://stackoverflow.com/questions/2419749/jquery-get-selected-elements-outer-html:  use .clone() and .wrap() with .parent() and .html().  Here it is as an extension:

$(function() {
  $.fn.outerHTML = function() {
    return $(this).clone().wrap('<div></div>').parent().html();
  }
});

and use it like this:
  $("#myItem").outerHTML();

1 comment: