JavaScript - isEqualNode() method

revision:


Category : element

The isEqualNode() returns true if two elements (or nodes) are equal.

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

They have the same nodeType.
They have the same nodeName.
They have the same nodeValue.
They have the same nameSpaceURI.
They have the same childNodes with all the descendants.
They have the same attributes and attribute values.
They have the same localName and prefix.

Syntax :

        element.isEqualNode(node)
        node.isEqualNode(node)
    

Parameters:

node : required. The node to compare.

Examples:

        var item1 = document.getElementById("myList1").firstChild;
        var item2 = document.getElementById("myList2").firstChild;
        var x = item1.isEqualNode(item2);
    

Practical examples

example: check if two list items in two different lists are the same.

Click the buttons to compare the first item in two of the lists.



List 1:
  • Water
  • Milk
List 2:
  • Coffee
  • Tea
List 3:
  • Water
  • Fire

code:
                    <div>
                        <p>Click the buttons to compare the <strong>first item</strong> in two of the lists.</p>
                        <button onclick="equalFunction('myList1','myList2')">Compare List 1 and 2</button>
                        <button onclick="equalFunction('myList1','myList3')">Compare List 1 and 3</button>
                        <br><br>
                        List 1:
                        <ul id="myList1"><li>Water</li><li>Milk</li></ul>
                        List 2:
                        <ul id="myList2"><li>Coffee</li><li>Tea</li></ul>
                        List 3:
                        <ul id="myList3"><li>Water</li><li>Fire</li></ul>
                        
                        <p id="equal-1" style="color: red"></p>
                    </div>
                    <script>
                        function equalFunction(x,y) {
                            var item1 = document.getElementById(x).firstChild;
                            var item2 = document.getElementById(y).firstChild;
                            var x = item1.isEqualNode(item2);
                            document.getElementById("equal-1").innerHTML = x;
                        }
                    </script>