jQuery contains() Method

Last Updated:

jQuery contains() Method

The jQuery contains() method checks whether a DOM element is a descendant of another DOM element.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery contains() Method</h1> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ alert($.contains(document.documentElement, document.body)); alert($.contains(document.body, document.documentElement)); }); }); </script> </body> </html>

Syntax

$.contains(container, contained);

Parameter Values

Value Type Explanation
container Required Specifies the DOM element that may contain the other element.
contained Required Specifies the DOM element that may be contained by (a descendant of) the other element.

Return Values

ValueExplanation
true The DOM element is a descendant of another DOM element.
false The DOM element is NOT a descendant of another DOM element.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
contains()YesYesYesYesYesYesYesYesYesYesYesYesYes

Share this Page

Meet the Author