You are here: Reference > JavaScript > client-side > selection and ranges > methods > containsNode (selectionRange)

containsNode method (selectionRange)

Browser support:
Returns whether the specified node is a part of the current selectionRange object.
The selectionRange object represents the current selection, so the containsNode method provides a way to check whether a node is a part of the selection.
The containsNode method always returns false in Opera from version 9.5. For more flexible functionality, use the compareBoundaryPoints method instead.

Syntax:

object.containsNode (nodeToCheck, partialAllow);
You can find the related objects in the Supported by objects section below.

Parameters:

nodeToCheck
Required. Reference to the node to check.
partialAllow
Required. Boolean that indicates whether intersection is allowed.
One of the following values:
false
The containsNode method returns true only if the entire contents of the node is part of the current selectionRange object.
true
The containsNode method returns true only if the node and the current selectionRange object have an intersection.

Return value:

Boolean. One of the following values:
false The specified node is not a part of the current selectionRange object.
true The specified node is a part of the current selectionRange object.

Example HTML code 1:

This example illustrates the use of the containsNode method:
<head>
    <script type="text/javascript">
        function TestPlacement () {
            var boldText = document.getElementById ("boldText");

            if (window.getSelection) {  // all browsers, except IE before version 9
                var selection = window.getSelection ();
                if (selection.containsNode) {
                    if (selection.containsNode (boldText, true)) {
                        alert ("The selection intersects the bold text.");
                    }
                    else {
                        alert ("The selection and the bold text have no intersection.");
                    }
                }
                else {
                    alert ("Your browser does not support this example!");
                }
            }
            else {
                alert ("Your browser does not support this example!");
            }
        }
    </script>
</head>
<body>
    Select some text on this page and use the following button to get information about 
    the placement of the <b id="boldText">bold text</b> relative to the selection.
    <br /><br />
    <button onclick="TestPlacement ();">Test placement</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content