If you wrap the container in a dummy P
tag you will get the container HTML also.
All you need to do is
var x = $('#container').wrap('<p/>').parent().html();
Check working example at http://jsfiddle.net/rzfPP/68/
To unwrap()
the <p>
tag when done, you can add
$('#container').unwrap();
var x = $('#container').get(0).outerHTML;
UPDATE : This is now supported by Firefox as of FireFox 11 (March 2012)
As others have pointed out, this will not work in FireFox. If you need it to work in FireFox, then you might want to take a look at the answer to this question : In jQuery, are there any function that similar to html() or text() but return the whole content of matched component?
I like to use this;
$('#container').prop('outerHTML');
var x = $('#container')[0].outerHTML;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With