Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery enclose current element in a new parent container

I want to enclose using jquery around the current element a new parent container, something like that, but I don't like the following lines and I suppose that they are not really correct or best practice:

this.before('<div id="container">');
this.after('</div>');
// do something with the new parent here
$("#container")...

Is there a way more natural of doing this? Thank you.

like image 725
m.john Avatar asked Jun 29 '11 10:06

m.john


1 Answers

You want to use the wrap function that is built for this!

this.wrap('<div id="container"></div>');

http://api.jquery.com/wrap/

like image 110
philnash Avatar answered Oct 04 '22 00:10

philnash