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.


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:

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

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

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter