How to insert a new div that 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 two existing paragraphs with a new div using JavaScript?

Desired Result:

<h1>Lorem ipsum!</h1>
<div>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. </p>
<p>Aliquam erat volutpat.</p>
</div>
+3
source share
4 answers

See this question for a solution using regular JavaScript.

+1
source

jquery has a method wrapif you use it (and if not, why not :))

+2
source

wrapAll().

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

( , jquery *)

0

Source: https://habr.com/ru/post/1756625/


All Articles