Reputation: 13853
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
Reputation: 186572
https://developer.mozilla.org/en/insertbefore or https://developer.mozilla.org/en/appendchild if you want to manually do it.
Upvotes: 0
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
Reputation: 32184
See this question for a solution using plain JavaScript.
Upvotes: 1
Reputation: 10748
jquery has a wrap
method if you are using it (and if not, why not :) )
Upvotes: 2