{"name":"has","type":"method","title":".has()","deprecated":null,"removed":null,"desc":"Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.","categories":["traversing/filtering","version/1.4"],"entries":[{"return":"jQuery","signatures":[{"added":"1.4","argument":{"desc":"A string containing a selector expression to match elements against.","name":"selector","type":"String"}},{"added":"1.4","argument":{"desc":"A DOM element to match elements against.","name":"contained","type":"Element"}}],"examples":{"desc":"Check if an element is inside another.","code":"\n$( \"ul\" ).append( \"
  • \" +\n ( $( \"ul\" ).has( \"li\" ).length ? \"Yes\" : \"No\" ) +\n \"
  • \" );\n$( \"ul\" ).has( \"li\" ).addClass( \"full\" );\n","css":"\n .full {\n border: 1px solid red;\n }\n","html":"\n\n"},"longdesc":"\n

    Given a jQuery object that represents a set of DOM elements, the .has() method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against the descendants of the matching elements; the element will be included in the result if any of its descendant elements matches the selector.

    \n

    Consider a page with a nested list as follows:

    \n
    \n <ul>\n  <li>list item 1</li>\n  <li>list item 2\n    <ul>\n      <li>list item 2-a</li>\n      <li>list item 2-b</li>\n    </ul>\n  </li>\n  <li>list item 3</li>\n  <li>list item 4</li>\n</ul>\n    
    \n

    We can apply this method to the set of list items as follows:

    \n
    \n$( \"li\" ).has( \"ul\" ).css( \"background-color\", \"red\" );\n    
    \n

    The result of this call is a red background for item 2, as it is the only <li> that has a <ul> among its descendants.

    \n "}]}