jQuery | contains() Method

This contains() method in jQuery is used to check whether a DOM element is a descendant of another DOM element or not.

Syntax:

jQuery.contains( container, contained )

Parameters: This method accepts two parameters as mentioned above and described below:

  • container: This parameter holds the DOM element that may contain the other element.
  • contained: This parameter holds the DOM element that may be contained by (a descendant of) the other element.

Return Value: It returns the boolean value.

Below examples illustrate the use of jQuery.contains() method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>JQuery | contains() method</title>
  
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
  
    <h3>JQuery | contains() method</h3>
  
    <b id="gfg"></b>
  
    <script>
        $("#gfg").append(
            "Is 'document.documentElement' in 'document.body'? <br>"
            + $.contains(document.documentElement, document.body));
  
    </script>
</body>
  
</html>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>JQuery | contains() method</title>
  
    <script src=
    </script>
  
</head>
  
<body style="text-align:center;">
  
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
  
    <h3>JQuery | contains() method</h3>
  
    <b id="gfg"></b>
  
    <script>
        $("#gfg").append(
            "Is 'document.body' in 'document.documentElement'? <br>"
            + $.contains(document.body, document.documentElement));
    </script>
</body>
  
</html>

chevron_right


Output:




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.