0

Possible Duplicate:
Get selected element's outer HTML

I suspect this is a really stupid question, but when you call the Html() function in JQuery it returns the selected nodes inner Html. Is there anyway of including the selected note in the returned Html? For example on the following HTML

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>

this call $('div.demo-container').html(); returns

<div class="demo-box">Demonstration Box</div>

How do I get it to return

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>

Thanks for the help!

Community
  • 1
  • 1
user460667
  • 1,870
  • 3
  • 19
  • 26

4 Answers4

2

You could wrap a copy of it in a boring div and get the HTML of that:

var str = $('div.demo-container').clone().wrap('<div/>').parent().html();
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
1

Write this extension, example from here

jQuery.fn.outerHTML = function(s) {
   return (s)
   ? this.before(s).remove()
   : jQuery("&lt;p&gt;").append(this.eq(0).clone()).html();
}

Then when you want to use it:

$('#myTag').outerHTML();
Gabe
  • 49,577
  • 28
  • 142
  • 181
0
$('div.demo-container').parent().html() //Only works in this specific situation

or

$('div.demo-container')[0].outerHTML; //Only works in IE and Chrome
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

This may work for you:

$('div.demo-container').parent().html();
jbabey
  • 45,965
  • 12
  • 71
  • 94