3

Say I have

<div id="controller">
 <div id="first">1</div>
 <div id="second">2</div>
</div>

$('#controller').html().which returns

<div id="first">1</div>
<div id="second">2</div>

How do I get .html() to return

<div id="controller">
 <div id="first">1</div>
 <div id="second">2</div>
</div>

Or is there an alternate function for that?

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
ade19
  • 1,150
  • 4
  • 13
  • 28
  • Have a look at this [post](http://stackoverflow.com/a/4741203/1577396) – Mr_Green Dec 28 '12 at 06:44
  • possible duplicate of [full HTML of object returned by jQuery selector](http://stackoverflow.com/questions/3535284/full-html-of-object-returned-by-jquery-selector) – DocMax Dec 28 '12 at 06:44

2 Answers2

3

Wrap it (ie. a clone) inside another parent

$('<div></div>').append($('#controller').clone()).html();

Also, check out a similar question.

Community
  • 1
  • 1
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
2

You need to use outerHTML

Live Demo

$('#controller')[0].outerHTML

You can add your div's clone to dynamically created div and use html of it.

$('<div>').append($('#controller').clone()).html();
Adil
  • 146,340
  • 25
  • 209
  • 204