AdamJones
AdamJones

Reputation: 601

Jquery - remove an element and children when an exact match only

I have some html that i need to try and tidy up. Sometimes the text contains contains paragraph tags that contain only a single break tag inside of them. This html isn't needed and I want to delete these paragraphs and their child break tags from specific selectors in my html.

I had understood that the following may work

$(".selectedClass p>br").remove();

But it seems to delete breaks from paragraphs containing breaks among other things. It's vital my removal only occurs when the html is exactly <p><br/></p>

If the paragraph contains other html/text then its valid and should stay.

Thanks for any help

Upvotes: 0

Views: 115

Answers (1)

j08691
j08691

Reputation: 208002

How about:

$('p').each(function () {
    if ($.trim($(this).html()) === '<br>') $(this).remove();
});

jsFiddle example

Upvotes: 3

Related Questions