Faiz Ahmed
Faiz Ahmed

Reputation: 251

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>

Upvotes: 3

Views: 92

Answers (3)

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33870

This should work :

$('p').wrap('<div class="p_wrapper">');

Upvotes: 2

Spokey
Spokey

Reputation: 10994

$('p').wrap('<div class="p_wrapper"></div>');

Upvotes: 3

Curtis
Curtis

Reputation: 103338

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

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

Upvotes: 7

Related Questions