Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: outer html() [duplicate]

Tags:

jquery

imagine what we have something like this:

<div id="xxx"><p>Hello World</p></div>

if we call .html function in this way:

$("#xxx").html();

we will get:

<p>Hello World</p>

But i need to get:

<div id="xxx"><p>Hello World</p></div>

So, what i need to do? I think to add another wrapper around #xxx, but this is not a good idea.

like image 409
vorobey Avatar asked Apr 21 '11 12:04

vorobey


2 Answers

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.

like image 166
Andy Avatar answered Nov 20 '22 06:11

Andy


Create a temporary element, then clone() and append():

$('<div>').append($('#xxx').clone()).html();
like image 216
David Tang Avatar answered Nov 20 '22 07:11

David Tang