Forums

Get help. Give help. A Web Design and Development Community.

Jquery: If ul has no li

  • # January 15, 2013 at 6:41 pm

    Hi Guys,

    I am trying to work out how to check if a UL DOES NOT contain a li … then if it is, add a message to say “sorry this is empty”.

    Any ideas?

    # January 15, 2013 at 6:55 pm

    This should do the trick
    http://codepen.io/karlpcrowley/pen/JboEi

    # January 15, 2013 at 6:58 pm

    Super minor optimization: http://codepen.io/ggilmore/pen/biehl

    # January 15, 2013 at 7:07 pm

    Ah yes, this is why I refactor my code, I always miss ! vs ==0

    # January 15, 2013 at 7:23 pm

    Perfect guys. Thanks so much for your help.

    I’m new to jQuery but very quickly realising the possibilities it opens.

    # July 21, 2016 at 5:43 am

    Hi,
    I was trying this same jQuery code

    if( !$(“ul”).has(“li”).length ) {
    $(“ul”).html(“Sorry, this is empty”);
    }

    with two UL one is empty and the other one has LI inside it.

    What we can do for this?

    I need this for my code. Please help!

    # July 21, 2016 at 8:47 am

    You might want to show us your code…

    # July 21, 2016 at 9:40 am

    Hi Shikkediel,
    Like this one –

    < ul >

    <∕ul >

    <ul >
    <li > some data
    <∕li >
    <∕ul >

    # July 21, 2016 at 1:11 pm

    Okay, as simple as that… give this a try then:

    $('ul').each(function() {
    
        if(!$(this).has('li').length) $(this).html('Sorry, this is empty');
    });
    
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

We have a pretty good* newsletter.