Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to make empty <li> tags hide on page load

My current code is:

<ul>
<li class="nav">
<-- uc tag with hyperlink that show/hides based on a code behind & user rights. --><br />
</li>
</ul>

<script type="text/javascript" >
    $('li.nav:empty').hide();       
</script>

The line elements automatically populate in the application based upon user rights. There are multiple instances of this type of link within the navigation.

This solution worked in a different application that I was working on, but for some reason isn't working on my current application.

The code above is only an example - not a direct copy and paste. The BR tags are not present in the actual application nor is the filler text.

like image 782
Sabra Avatar asked Nov 21 '25 07:11

Sabra


2 Answers

First you need to remove the
from inside the li ..

Enclose your script in DOM ready Handler

<script type="text/javascript">
   $(function () {
      $('li.nav:empty').hide();
    });       
</script>
like image 155
Sushanth -- Avatar answered Nov 23 '25 00:11

Sushanth --


If you want to hide li elements with no text, you can use this :

$('li.nav').filter(function(){return $(this).text().trim().length==0}).hide();

Demonstration

like image 28
Denys Séguret Avatar answered Nov 22 '25 22:11

Denys Séguret