Strip p tags if it surrounds an image link?

The platform on which I create the site adds tags paround image links in wysiwyg mode, and this will ruin the layout. Anyway, can I use jQuery to break tags pif it surrounds the image?

<div class="post">
    <p><a href="link"><img src="image"></a></p>
    <p><a href="link"><img src="image"></a></p>
</div>
+3
source share
1 answer
$('p > a > img').parent().unwrap();

Example: http://jsfiddle.net/6z4Sv/

If there is some variation in the hierarchy inside the elements p, you can do this:

$('p img').closest('p > *').unwrap();

Example: http://jsfiddle.net/6z4Sv/1/

And you can use div.postat the beginning of the selector if you want to work only on elements pthat descend from this class.

.

+6

All Articles