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
  • 180,066
  • 36
  • 283
  • 292
ade19
  • 1,085
  • 4
  • 13
  • 27
  • 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
  • 47,885
  • 24
  • 119
  • 191
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
  • 143,427
  • 25
  • 201
  • 198