Forums

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

Home Forums JavaScript Counting list items in each TD with jQuery?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #35672
    mikes02
    Participant

    Within each TD of the retailers div there is a UL, what I am trying to do is determine how many list items exist in each individual TD, if there is more than 1 list item I need to style it a specific way, as you can see below, however, when trying this code it applies the CSS to every LI, I am not sure where I am going wrong?

    	$('#retailers tr').each(function()
    {
    $.each(this.cells, function()
    {
    var countLI = $("ul.links").children().length;

    if(countLI + ":gt(1)")
    {
    $("ul.links li").css("background-color", "yellow");
    }
    });
    });
    #92896
    Mottie
    Member

    Just from a quick glance at the code, “:gt(1)” is a jQuery selector and shouldn’t be used in a numerical comparison. Try this:

            $('#retailers tr').each(function()
    {
    $.each(this.cells, function()
    {
    var countLI = $(this).find("ul.links").children();

    if(countLI.length > 1)
    {
    countLI.css("background-color", "yellow");
    }
    });
    });
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.