Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap an element with a div in jQuery?

What I want is to wrap all my p tags in a special div having a class p_wrapper. I want something like this:(For all elements not only for p and div)

$('p').wrappAll('.p_wrapper')

I should get this HTML:

Before

<p>This is a paragraph!</p>
<p>This is another paragraph!</p>

After

<div class="p_wrapper"> <p>This is a paragraph!</p> </div>
<div class="p_wrapper"> <p>This is another paragraph!</p> </div>
like image 569
Faiz Ahmed Avatar asked Dec 28 '25 17:12

Faiz Ahmed


1 Answers

$("p").wrap($("<div/>").addClass("p_wrapper")); 

http://jsfiddle.net/aXwDs/1/

like image 129
Curtis Avatar answered Dec 31 '25 07:12

Curtis