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.

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

+2, But OP: be careful that the child doesn't already contain a <p> that will get wrapped with another <p> when you append it to the surrogate div (i.e., $a.html() == '<p>Old content of child div.</p>';).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.