jQuery: outer html()
Just use standard DOM functionality:
$('#xxx')[0].outerHTML
Or a bit simpler with .prop()
:
$('#xxx').prop('outerHTML')
outerHTML
is well supported - verify at Mozilla or caniuse.
No siblings solution:
var x = $('#xxx').parent().html();
alert(x);
Universal solution:
// no cloning necessary
var x = $('#xxx').wrapAll('<div>').parent().html();
alert(x);
Fiddle here: http://jsfiddle.net/ezmilhouse/Mv76a/
Create a temporary element, then clone()
and append()
:
$('<div>').append($('#xxx').clone()).html();