views:

146

answers:

1

What's the best way to select the html of an element, inclusive? For example:

<div id="testDiv" class="testClass">
  This is just a test.
</div>

Whereas $('#testDiv').html() returns "This is just a test.", I would a selector that returns:

<div id="testDiv" class="testClass">This is just a test.</div>.

Thank you for your replies.

+3  A: 

Try this jQuery.outerHTML() implementation.

jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};
Gumbo