You are here: Reference > JavaScript > client-side > HTML DOM > methods > isSameNode

isSameNode method

Browser support:
9
Returns whether the current node is the same node as the specified one.
When we speak about nodes in JavaScript, it exactly means node object references. Two node references are the same if they reference to the same object. The strict equal operator (===) is equivalent to the isSameNode method for nodes in JavaScript.

There is a similar method to the isSameNode method in JavaScript, the isEqualNode method. It tests the equality of two node references. When two node references are the same then they are also equal, but different node references can also be equal. For detailed description, please see the page for the isEqualNode method.

If you need to compare the positions of two elements in the document hierarchy:

Syntax:

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

Parameters:

nodeToCheck
Required. Reference to the node to compare.

Return value:

Boolean. One of the following values:
false The specified node is not the same as the current element.
true The specified node is the same as the current element.

Example HTML code 1:

This example illustrates the use of the isSameNode method:
<head>
    <script type="text/javascript">
        var lastButton = null;
        function Test (button) {
            if (lastButton) {
                if (button.isSameNode) {
                    if (button.isSameNode (lastButton)) {
                        alert ("Last time you clicked on the same button.");
                    }
                    else {
                        alert ("You clicked on another button before.");
                    }
                }
                else {
                    alert ("Your browser does not support the isSameNode method.");
                }
            }
            lastButton = button;
        }
    </script>
</head>
<body>
    Click on the buttons!
    <button onclick="Test (this);">button1</button>
    <button onclick="Test (this);">button2</button>
    <button onclick="Test (this);">button3</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example implements a cross-browser solution for the previous example:
<head>
    <script type="text/javascript">
        var lastButton = null;
        function Test (button) {
            if (lastButton) {
                if (button === lastButton) {
                    alert ("Last time you clicked on the same button.");
                } 
                else {
                    alert ("You clicked on another button before.");
                }
            }
            lastButton = button;
        }
    </script>
</head>
<body>
    Click on the buttons!
    <button onclick="Test (this);">button1</button>
    <button onclick="Test (this);">button2</button>
    <button onclick="Test (this);">button3</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content