JavaScript node.isSameNode() Method

You are Here:

JavaScript node.isSameNode() Method

The node.isSameNode() method tests whether two nodes are the same.

Note: Two nodes are same if both nodes have the same object as reference.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>This is paragraph 1</p> <p>This is paragraph 2</p> <p>This is paragraph 1</p> <button onclick="p1ANDp1()">Compare 1 & 1</button> <button onclick="p1ANDp2()">Compare 1 & 2</button> <button onclick="p1ANDp3()">Compare 1 & 3</button> <script> var p1 = document.getElementsByTagName("p")[0]; var p2 = document.getElementsByTagName("p")[1]; var p3 = document.getElementsByTagName("p")[2]; function p1ANDp1(){ alert(p1.isSameNode(p1)); } function p1ANDp2(){ alert(p1.isSameNode(p2)); } function p1ANDp3(){ alert(p1.isSameNode(p3)); } </script> </body> </html>

Syntax

node.isSameNode(otherNode)

Parameter Values

ValueTypeExplanation
otherNodeRequiredThe Node to test against.

Return Values

ValueExplanation
trueIf both the elements are same element.
falseIf both the elements are different element.

isEqualNode() vs isSameNode()

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>This is paragraph 1</p> <p>This is paragraph 2</p> <p>This is paragraph 1</p> <button onclick="myFunction()">Compare All</button> <script> var p1 = document.getElementsByTagName("p")[0]; var p2 = document.getElementsByTagName("p")[1]; var p3 = document.getElementsByTagName("p")[2]; function myFunction(){ console.log("p1 vs p1 isEqualNode() "+p1.isEqualNode(p1)); console.log("p1 vs p1 isSameNode() "+p1.isSameNode(p1)); console.log("p1 vs p2 isEqualNode() "+p1.isEqualNode(p2)); console.log("p1 vs p2 isSameNode() "+p1.isSameNode(p2)); console.log("p1 vs p3 isEqualNode() "+p1.isEqualNode(p3)); console.log("p1 vs p3 isSameNode() "+p1.isSameNode(p3)); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author