JavaScript node.isEqualNode() Method

You are Here:

JavaScript node.isEqualNode() Method

The node.isEqualNode() method tests whether two nodes are equal.

Note: Two nodes are equal if all the following conditions are true

  • Both nodes should have the same Node Type.
  • Both nodes should have the same attributes and attribute values (the attributes does not have be in the same order).
  • Both nodes should have the same nodeName, NodeValue, localName, nameSpaceURI and prefix.
  • Both nodes should have the same childNodes with all the descendants.

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.isEqualNode(p1)); } function p1ANDp2(){ alert(p1.isEqualNode(p2)); } function p1ANDp3(){ alert(p1.isEqualNode(p3)); } </script> </body> </html>

Syntax

node.isEqualNode(otherNode)

Parameter Values

ValueTypeExplanation
otherNodeRequiredThe Node to compare equality with.

Return Values

ValueExplanation
trueIf the content of both the elements are same.
falseIf the content of both the elements are different.

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