KatieK
KatieK

Reputation: 13853

How To Insert a New Div Which Surrounds Existing Elements

Given this markup:

<h1>Lorem ipsum!</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. </p>
<p>Aliquam erat volutpat.</p>

How can I surround the two existing paragraphs with a new div using JavaScript?

Desired outcome:

<h1>Lorem ipsum!</h1>
<div>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. </p>
<p>Aliquam erat volutpat.</p>
</div>

Upvotes: 3

Views: 1796

Answers (4)

jstoneky
jstoneky

Reputation: 1

You should add a class to the paragraph tags and use the wrapAll() function.

$(".yourPclass").wrapAll("<div/>");

(This was making an assumption if you were using jquery*)

Upvotes: 0

Niels van der Rest
Niels van der Rest

Reputation: 32184

See this question for a solution using plain JavaScript.

Upvotes: 1

&#181;Bio
&#181;Bio

Reputation: 10748

jquery has a wrap method if you are using it (and if not, why not :) )

Upvotes: 2

Related Questions