Is there a way to check if two DOM elements are equal?

Unable to find element by position and element position in Javascript. But is there a general way to compare them?

The only way I could think of is to compare identifiers or class names, but not all elements have identifiers or class names.

+43
javascript dom
Sep 06 2018-10-10 at
source share
2 answers

If you want to compare two pointers to an element in order to be the same element, just use the comparison operator. This is easy to prove because

document.body === document.body 

For example, if I somehow had links to two elements that I did not know:

 if (element1 === element2) ... 
+37
Sep 06 '10 at 8:15
source share

In modern browsers, there are two methods for comparing nodes.

 var a = document.createElement('div'); var b = document.createElement('div'); b.isEqualNode(a); // true 

but

 b.isSameNode(a); //false 

As for IE, DOM elements have the non-stanard attribute, uniqueID . But I can’t imagine that this can be useful in this case, since yes, you can compare two pointers.

+29
Mar 18 2018-11-21T00:
source share



All Articles