ivy ong
ivy ong

Reputation: 101

Remove outer element with jquery

<div>
    <p>
        <img>
    </p>
    <p></p>
    <p></p>
</div>

How to remove the first <p></p> but not the childs (img)?

Upvotes: 1

Views: 229

Answers (1)

A.B
A.B

Reputation: 20445

you can use .unwrap()

to unwrap the first p you have to specify the select as first() p also

 $("p").first().contents().unwrap()

Working Demo:

$("p").first().contents().unwrap()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div>
<p>
<img>
</p>
<p>aaa
</p>
<p>
</p>
</div>

Upvotes: 5

Related Questions