Node.contains

Table of contents

    1. 1.1. Summary
    2. 1.2. Syntax
    3. 1.3. Example
  1. 2. Browser compatibility
    1. 2.1. Specification
Table of contents
    1. 1.1. Summary
    2. 1.2. Syntax
    3. 1.3. Example
  1. 2. Browser compatibility
    1. 2.1. Specification

« Gecko DOM Reference

Summary

Indicates whether a node is a descendent of a given node.

Syntax

node.contains( otherNode ) 
  • node is the node that's being compared.
  • otherNode is the node that's being compared against.

The return value is true if otherNode is a descendent of node, or node itself. Otherwise the return value is false.

Example

This function checks to see if an element is in the page's body. As contains is inclusive and determining if the body contains itself isn't the intention of isInPage this case explicitly returns false.

function isInPage(node) {
  return (node === document.body) ? false : document.body.contains(node);
}
  • node is the node we want to check for in the <body>.

Browser compatibility

  • Desktop
  • Mobile

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 9.0 (9.0) (Yes) ? (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? 9.0 (9.0) ? ? ?

Specification

DOM Level 4: contains

Tags (3)

Edit tags

Attachments (0)

 

Attach file