Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if two elements are equal in JavaScript

Tags:

javascript

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?

like image 587
agconti Avatar asked Apr 20 '14 01:04

agconti


People also ask

What is === in JavaScript?

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.

How can you tell if two elements are the same?

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.

Can we check equality of two nodes JavaScript?

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.

Are objects equal JavaScript?

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 === .


2 Answers

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.
like image 122
Mike Samuel Avatar answered Sep 28 '22 06:09

Mike Samuel


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
like image 24
fejese Avatar answered Sep 28 '22 05:09

fejese