theorise
theorise

Reputation: 7425

How do I select an individual element inside an if { } in jQuery?

I want to be able to hide a selector if it contains any data.

HTML

<ul>
    <li></li>
    <li>Test</li>
    <li></li>
</ul> 

JS

   var x= $('#ul li');

   if (x.html().length > 0) {
        $(this).hide();
   } 

The $(this) isn't working, but I have no idea how to select it.

Upvotes: 0

Views: 70

Answers (1)

BoltClock
BoltClock

Reputation: 723678

You'll want to iterate them using .each(), then check the inner HTML of each element (that's where $(this) makes sense in your case):

x.each(function() {
    if ($(this).html().length > 0)
        $(this).hide();
});

Upvotes: 5

Related Questions