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
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
Share Improve this question asked Jan 9, 2010 at 4:40 HcabnettekHcabnettek 12.9k38 gold badges129 silver badges192 bronze badges 2- Is there any error with the above code? – rahul Commented Jan 9, 2010 at 4:45
- nope. not syntactically, leastways. – jrharshath Commented Jan 9, 2010 at 4:48
5 Answers
Reset to default 2$("#something").wrap("<div id='newParent'></div>");
$("#someDiv").wrap("<div id='newParent'></div>");
Sure. You can do anyting in jquery. check out jQuery.wrap
Assuming someDiv is already appended to the document...
$('#someDiv').wrap('<div>')
This would wrap the element with a newly created division. Documentation.
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.