Related Articles

Related Articles

jQuery | contains() Method
  • Last Updated : 14 May, 2020

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
Recommended Articles
Page :