jQuery | has() with Examples

The has() is an inbuilt method in jQuery which is used to find all the elements inside the specified list of elements.
Syntax:

$(selector).has(element)

Parameter: It accepts a parameter expression or an element to match elements against them.
Return Value: It returns all elements that match the specified selector having one or more elements inside them.

jQuery code to show the working of has() method:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <script 
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function() {
            $("p").has("span").css("background-color", "lightgreen", "bold");
        });
    </script>
    <style>
        body {
            width: 50%;
            height: 100px;
            border: 2px solid green;
            padding: 15px;
            font-size: 19px;
        }
    </style>
</head>
  
<body>
  
    <p>Geeks <span>for</span> Geeks !</p>
    <p>I am a <span>content</span> writer.</p>
    <p>This is a normal paragrap .</p>
  
</body>
  
</html>

chevron_right


Output:
All the “p” elements containing the “span” elements get highlighted.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.