1

I am filling a list up dynamically from a database, the fill up works fine, but where I have no data the list item is empty. I tried to hide the empty rows, bot my code does nothing, where should I put it if it is correct? The div is filled in from the document ready function, and after the div that contains this one, shows on mouseclick.

if ($("#results").find('.list-group-item').length() == 0){
            $(this).css('height','0px');
1
  • 3
    Check your console log. length is a property, not a function. Commented Oct 29, 2015 at 11:28

2 Answers 2

4

You could use the CSS3 :empty selector, it's cleaner:

#results:empty{
    display: none;
}

In order to :empty to match your list it must be completely empty, there must be no white spaces or line breaks. Like <ul id="results"></ul>

Or to just hide the <li> elements:

.list-group-item:empty{
    display:none;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to hide the whole UL, I yust want to hide the empty <li class="list-group-item"></li> element.
In that case use another selecter targeting each <li>, I'll update the answer.
2

You can use filter() to filter empty list

$("#results").find('.list-group-item').filter(function() {
  return $.trim($(this).text()) == '';
}).hide()

2 Comments

Did you mean to make this a snippet, or just a code block?
Or just use api.jquery.com/empty-selector $("#results .list-group-item:emty").hide()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.