How to find if LI has children UL

Viewed 69729

I have the following structure:

<ul>
    <li class="static">
        <ul class="static">
        </ul>
    </li>
    <li class="static"></li>
</ul>

As you can see the first LI element has UL inside it but the next one doesn't. Is there a way to find out through jquery if a certain LI has UL inside it or not? I want to do something like this:

if(li has children ul)
{
    do something
}

EDIT

I tried the following but it shows "YES" for all cases. Here's my code and HTML. In the HTML below, only "Link2" contains child UL and not Link1 and Link3. I just want to do some operation when a user clicks on some LI which contain child UL.

CODE

$('#DeltaPlaceHolderLeftNavBar div > div > ul > li > a').click(function()
{
   if($('li:has(> ul)'))
      alert("yes");
  else
     alert("no");
});

HTML

<div class="ms-core-navigation" id="DeltaPlaceHolderLeftNavBar">
 <div id="ctl00_PlaceHolderLeftNavBar_QuickLaunchNavigationManager">
  <div class=" noindex ms-core-listMenu-verticalBox" id="zz14_V4QuickLaunchMenu">
   <ul class="root ms-core-listMenu-root static" id="zz15_RootAspMenu">
    <li class="static">
     <a href="link1.php" tabindex="0" class="someclass1">
      <span class="someclass2">
       <span class="menu-item-text">Link1</span>
      </span>
     </a>
    </li>
    <li class="static">
     <a href="link2.aspx" tabindex="0" class="someclass3">
      <span class="someclass2">
       <span class="menu-item-text">Link2</span>
      </span>
     </a>
     <ul class="static">
      <li class="static">
       <a href="Link2A.php" tabindex="0" class="someclass1">
        <span class="someclass2">
         <span class="menu-item-text">Link2A</span>
        </span>
       </a>
      </li>
      <li class="static">
       <a href="Link2B.php" tabindex="0" class="someclass1">
        <span class="someclass2">
         <span class="menu-item-text">Link2B</span>
        </span>
       </a>
      </li>
     </ul>
    </li>
    <li class="static">
     <a href="Link3.php" tabindex="0" class="someclass1">
      <span class="someclass2">
       <span class="menu-item-text">Link3</span>
      </span>
     </a>
    </li>
   </ul>
  </div>
 </div>
</div>
8 Answers
jQuery(".shop-nav > div >ul > li > ul").prev("li").attr("data-toggle","dropdown");

I think each method is for you . Try to iterate over li to find if it has ul as a children or not, if yes then turn the paticular list background red or whatever you want.

  $('.top-menu > ul > li').each (function(){
    if($(this).children('ul').length) {
      $(this).css('background', 'red');
    }
  });
Related