How do I get the outerHTML of a jquery object? The following form is not working.
$("selector").outerHTML;
How do I get the outerHTML of a jquery object? The following form is not working.
$("selector").outerHTML;
A jQuery object has a reference to the native element you want and you can extract it with $('selector')[0]
, from there you can use native JS.
var outer = $('selector')[0].outerHTML;
Another option is to create a temporary element that inserts the first element and searches for .html()
. You may need this solution if you have multiple elements.
var outer = $('<div>').append($('selector').clone()).html();
or, without having to clone:
var outer = $('selector').wrapAll('<div>').parent().html();
Use
$("selector")[0].outerHTML;