AK4668
AK4668

Reputation: 1912

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

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>

Upvotes: 2

Views: 1396

Answers (5)

Animesh Nandi
Animesh Nandi

Reputation: 458

here is a demo for your questiov

jQuery("ul").siblings("br").each(function(){
jQuery(this).remove();});

demo

Upvotes: -1

Milan Jose
Milan Jose

Reputation: 3

Precisely from your reqirements

$('ul').next('br').remove();
$('ul').first('br').remove();

But, Alnitak has a more elegant way of doing it.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339955

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.

Upvotes: 6

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33880

Do not use JS only, use CSS display : none.

If someone desactive JS, the br will still be there. Do this instead :

ul > br{display : none};

If you want to remove it for a better source code, use this :

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

If you want to delete the br next to te ul, use thos selector instead :

CSS

ul + br

jQuery

$('ul + br')

Upvotes: 0

97ldave
97ldave

Reputation: 5249

To remove the <br /> after the <ul> is closed you can use this:

$("ul").next("br").remove();

To remove the <br /> inside the <ul>, you can use this:

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

Upvotes: 4

Related Questions