Adding elements with Jquery

0

I would like to know how I add elements above and below various paragraphs with jquery. Let's say, I have to get the third paragraph and add a <div> and I finish the </div> in the penultimate paragraph.

<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>

The result would be this:

    <p>1</p>
    <p>2</p>
    <div>
    <p>3</p>
    <p>4</p>
    <p>5</p>
    <p>6</p>
    </div>
    <p>7</p>

I tried with after and before , but the browser closes the tags.

    
asked by anonymous 17.03.2016 / 01:22

1 answer

1

I was able to accomplish what I wanted:

$("p").slice( 2, 5 ).wrapAll( '<div></div>' );
    
17.03.2016 / 01:52