Add elements with Jquery

Asked

Viewed 67 times

0

I would like to know how to add above and below several paragraphs with jquery. Say, I have to take the third paragraph and add a <div> and finalize 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.

1 answer

2

I was able to accomplish what I wanted:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.