Checking if two elements are equal in JavaScript -


why elementa === elementb produce different results elementb.isequalnode(elementa)?

after checking this answer on is there way check if 2 dom elements equal? i'm trying check if 2 elements equal in javascript using ===. surprisingly, when element a , b same a === b returns false while b.isequalnode(a) returns true.

here's 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 fiddle.

why case?

you create new element. not same existing one. equal though have same structure , content. it's 2 string objects equal if contain same text not same if created them separately.

here's simpler case:

var div1 = document.createelement('div'); var div2 = document.createelement('div'); alert(div1 === div2); // false alert(div1.isequalnode(div2)); // true 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -