Why does elementA === elementB produce different results then elementB.isEqualNode(elementA)?
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?