jQuery.contains()

jQuery

jQuery.contains()


jQuery.contains( container, contained ) Returns: Boolean

Description: Check to see if a DOM element is a descendant of another DOM element.

  • version added: 1.4jQuery.contains( container, contained )

    • container
      Type: Element
      The DOM element that may contain the other element.
    • contained
      Type: Element
      The DOM element that may be contained by (a descendant of) the other element.

The $.contains() method returns true if the DOM element provided by the second argument is a descendant of the DOM element provided by the first argument, whether it is a direct child or nested more deeply. Otherwise, it returns false. Only element nodes are supported; if the second argument is a text or comment node, $.contains() will return false.

Note: The first argument must be a DOM element, not a jQuery object or plain JavaScript object.

Example:

Check if an element is a descendant of another.

1
2
                                  
$.contains( document.documentElement, document.body ); // true
$.contains( document.body, document.documentElement ); // false