Let's say we have this HTML code:
<body>
<div>Some text</div>
<div>Some other text</div>
</body>
and I have a clone function ( that's what I am trying to make ) that clones an element or a set of elements, and we call the function like this:
clone( 'div' );
So the HTML become:
<body>
<div>Some text</div>
<div>Some other text</div>
<div>Some text</div>
<div>Some other text</div>
</body>
The clone part is easy, the problem is the inheritance of any events listeners applied to the divs
before the function is called. My idea is to store the event function every time an event listener is added in an object possibly like this:
evtList = [
{ elem: elemObject, type: eventType, handler: function },
...
...
];
and then every time I clone a node, I just check if there is an "equal" node (element) in the evtList so I can add the same event listener. And here is the real problem. When I try to check equality using "===" I get false every time. I did some searching and I found node.isEqual(cloneNode)
but this is not supported anymore. Is there any way to determine if two nodes (elements) are "equal"? ( in pure javascript of course )
EDIT:
I made a function isEq()
that checks if two objects are equal (deep), and it seems to work with any literal object I try. But, I noticed a weird behavior:
isEq( $('div')[0], $('div')[0] ); // Returns False
isEq( $('div').first(), $('div').first() ); // Returns True
Any idea why isEq()
behaves that way?