Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Jquery: If ul has no li Reply To: Jquery: If ul has no li

#245392
nejuzofeco
Participant

Please, don’t!

Don’t put a text message inside the ul tag. Please, don’t do this:

<ul>
    Sorry, this is empty (and wrong)!
</ul>

It’s invalid HTML. Permitted content for ul tag is zero or more li elements (https://developer.mozilla.org/en/docs/Web/HTML/Element/ul).

That is, you can’t put a piece of text in that place.

If you want to show this message, use javascript to replace the ul tag by, for instance, a p tag. Using jQuery, you can do:

$('ul').each(function() {
    // Will replace "<ul></ul>" by "<p>Sorry, this is empty</p>"
    if(!$(this).has('li').length) $(this).replaceWith('<p>Sorry, this is empty</p>');
});