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


Please, don’t!

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

    Sorry, this is empty (and wrong)!

It’s invalid HTML. Permitted content for ul tag is zero or more li elements (

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>');