views:

53

answers:

4

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>
+2  A: 

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

BioBuckyBall
+1  A: 

See this question for a solution using plain JavaScript.

Niels van der Rest
+1 - sAc's answer contains a nice solution and it was asked moments ago. We can't possibly be answering the same question for each new type of element :)
Anurag
Yes, I did have a déjà vu moment when I read this question. But as the other question was specifically about a non-jQuery solution, I didn't want to call this a duplicate :)
Niels van der Rest
Oh dear, my apologies. I didn't find that other question when I was searching. I've asked some follow-up questions in there.
KatieK
A: 

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*)

jstoneky
A: 

https://developer.mozilla.org/en/insertbefore or https://developer.mozilla.org/en/appendchild if you want to manually do it.

meder