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:
<!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 > |
Output:
Example 2:
<!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 > |
Output: