Search code examples
javascriptjqueryjquery-selectorschildren

How to get objects that have children of some type using jQuery?


How to get objects that have children of some specific type using jQuery? For example, having the next DOM:

<ul id="menu">
  <li>Item 1</li>
  <li>
    Item 2
    <ul>
      <li>Item 2.1</li>
      <li>Item 2.2</li>
    </ul>
  </li>
  <li>
    Item 3
    <ul>
      <li>
        Item 3.1
        <ul>
          <li>Item 3.1.1</li>
          <li>Item 3.1.2</li>
        </ul>
      </li>
      <li>Item 3.2</li>
    </ul>
  </li>

How can I get all the <li> elements inside #menu that have at least one child of type <ul>?


Solution

  • Try this -

    $("#menu li:has('ul')")
    

    http://api.jquery.com/has-selector/

    Working Demo --> http://jsfiddle.net/V6Eqm/