2

Possible Duplicate:
How to move an element into another element?

I basically want to reassign a parent to my DIV:

<DIV id='main'>
   <DIV id='child'>

   </DIV>
</DIV>
<DIV id='surrogate'>
</DIV>

So i want child to be child of surrogate.

I tried:

var $a = $('#child');
var contents = $a.contents();
$a.remove();
$('#surrogate').append('<p>' + contents + '</p>');

But it will just output: [object Object]

Is there a better/working way to just reassign a parent to a whole tree of elements, without reading the content copying or cloning it? Well, maybe cloning works, but I'd rather just move it.

Community
  • 1
  • 1
TrySpace
  • 2,233
  • 8
  • 35
  • 62

1 Answers1

2

You want to get the html from inside child which returns a string so you can concatenate it the way you are doing

var $a = $('#child');
$('#surrogate').append('<p>' + $a.html() + '</p>');

$a.remove();

contents() is returning a jQuery object and you can't concatenate an object and string

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • +2, But OP: be careful that the `child` doesn't already contain a `

    ` that will get wrapped with another `

    ` when you append it to the surrogate div (i.e., `$a.html() == '

    Old content of child div.

    ';`).
    – msanford Mar 07 '12 at 20:07