views:

39

answers:

5

Hi All,

I have a div #someDiv, can I create a new element and make #someDiv be its child? Basically I want to wrap a new element aound #someDiv. How do I do this?

$("#someDiv").appendTo($("<div id='newParent'></div>")); 
// The intent is to move #someDiv into the new container
// The new container may or may not be in the same 
// position in the DOM as #someDiv

Thanks for any tips!

Cheers, ~ck in San Diego

+2  A: 
$("#something").wrap("<div id='newParent'></div>");
Jonathan Sampson
A: 

Sure. You can do anyting in jquery. check out jQuery.wrap

Here Be Wolves
+1  A: 
$("#someDiv").wrap("<div id='newParent'></div>");
Tomalak
A: 

Assuming someDiv is already appended to the document...

$('#someDiv').wrap('<div>')

This would wrap the element with a newly created division. Documentation.

meder
A: 

if the newParent div is not created you'll want to use wrap.

$('somediv').wrap("<div id='newparent'></div>");

if it already exists, what you have will work.

Brandon H