Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Remove <br> from <ul>, <ol>

Tags:

jquery

I have a code generated by PHP which has unwanted br tags. How can I remove the <br> when appears immediately after opening <ul> ie: it is not inside <li> How can I remove <br> when it is immediately after closing tag of </ul>

<ul>
    <br>
    <li> ....</li>
    <li> ....</li>
    <li> ....</li>
</ul>
<br>
like image 314
AK4668 Avatar asked Mar 02 '26 15:03

AK4668


1 Answers

Seeing as the only legal child of a <ul> is an <li> tag:

$('ul').children(':not(li)').remove();

or if you want to be more specific and only address this specific error:

$('ul > br').remove();

To remove a <br> that follows a <ul>, you can use the "preceding" CSS selector:

$('ul + br').remove();

Note that the latter does not refer just to the "opening" <ul> tag, but the entire <ul> element up to and including its closing </ul> tag.

like image 93
Alnitak Avatar answered Mar 05 '26 05:03

Alnitak