After checking this answer on Is there a way to check if two DOM elements are equal? I'm trying to check if two elements are equal in javascript using ===
. Surprisingly, when element A
and B
are the same A === B
returns false
while B.isEqualNode(A)
returns true
.
Here's an example:
html:
<div>
<h1>Test</h1>
</div>
JavaScript:
var inMemoryDiv = document.createElement('div');
var inMemoryH1 = document.createElement('h1');
inMemoryH1.innerHTML = "Test";
inMemoryDiv.appendChild(inMemoryH1);
var h1 = document.getElementsByTagName('h1')[0];
alert(h1 === inMemoryH1); // false
alert(inMemoryH1.isEqualNode(h1)); // true
alert(h1.innerHTML === inMemoryH1.innerHTML); // true
Replicated in a fiddle.
Why is this the case?
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
Two different elements have similar chemical properties when they have the same number of valence electrons in their outermost energy level. Elements in the same column of the Periodic Table have similar chemical properties.
The isSameNode() method checks if two nodes are the same node. The isSameNode() method returns true if the two nodes are the same node, otherwise false. Tip: Use the isEqualNode() method to check if two nodes are equal, but not necessarily the same node.
Even if two object has same key and value pair it will return false. As you can see above example, both name and fullName are identical. Yet, the object are not equal either with == or === .
isEqualNode
is defined in DOM Level 3 thus:
This method tests for equality of nodes, not sameness (i.e., whether the two nodes are references to the same object) which can be tested with
Node.isSameNode()
. All nodes that are the same will also be equal, though the reverse may not be true.Two nodes are equal if and only if the following conditions are satisfied:
- The two nodes are of the same type.
- The following string attributes are equal: nodeName, localName, namespaceURI, prefix, nodeValue. This is: they are both null, or they have the same length and are character for character identical.
- The attributes NamedNodeMaps are equal. This is: they are both null, or they have the same length and for each node that exists in one map there is a node that exists in the other map and is equal, although not necessarily at the same index.
- The childNodes NodeLists are equal. This is: they are both null, or they have the same length and contain equal nodes at the same index. Note that normalization can affect equality; to avoid this, nodes should be normalized before being compared.
You create a new element. That is not the very same as the already existing one. They are equal though as they have the same structure and content. It's like two string objects are equal if they contain the same text but not the same if you created them separately.
Here's an even simpler case:
var div1 = document.createElement('div');
var div2 = document.createElement('div');
alert(div1 === div2); // false
alert(div1.isEqualNode(div2)); // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With