jQuery .has() method checks existence for a selector

Syntax and Description

The .has() method constructs a new jQuery object from a subset of the matching elements.

.has(selector)

selector is a string containing a selector expression to match elements against.

Return value is the new jQuery object.

Consider a page with a nested list as follows:


<ul>/* w w w .  jav a 2 s.  c om*/
 <li>1</li>
 <li>2
     <ul>
         <li>list item 2-a</li>
         <li>list item 2-b</li>
     </ul>
 </li>
 <li>3</li>
 <li>4</li>
</ul>

The following code

$('li').has('ul').css('background-color', 'red');

marks item 2 to red.





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities