.has()

In this chapter you will learn:

  1. Syntax and Description for .has() method

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>/*from j ava  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.

Next chapter...

What you will learn in the next chapter:

  1. Syntax and Description for .hasClass() method
  2. How to check if an element has a certain class
  3. Verify the existence of a class just added
  4. How to combine hasClass methods to filter