tags:

views:

45

answers:

3

How can I set display:none to a ul if it has only one child?

Eg, this would be hidden:

<ul id="section">
  <li id="inThis">In this section:</li>
</ul>

But this would not:

<ul id="section">
   <li id="inThis">In this section:</li>
   <li>Item 1</li>
   <li>Item 2</li>
</ul>

Thanks!

A: 
if($("#section li").length == 1)
{
   $("#section").hide();
}
TheVillageIdiot
Nice and compact. Thank you!
Rick
no need for the ==1
redsquare
A: 

//this is one way

if($('#section li').length == 1){
  $j(this).parent().css('display', 'none');
}
Dhana
So many ways to do the one thing. Thank you :)
Rick
A: 
$("ul li:only-child").parent().hide();
Joe Chung