Reputation:
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.
Upvotes: 5
Views: 1140
Reputation: 1479
This is similar to the accepted answer, adding 2 improvements:
Here is the code:
// add outer html selector
(function ($) {
$.fn.outerHTML = function() {
return $('<div>').append( this.eq(0).clone() ).html();
return this;
};
}(jQuery));
Upvotes: 0
Reputation: 655309
Try this jQuery.outerHTML()
implementation.
jQuery.fn.outerHTML = function() {
return $('<div>').append( this.eq(0).clone() ).html();
};
Upvotes: 5