How to compare two HTML elements
html, javascript, jquery
Solution
You can use:
element1.isEqualNode(element2);
In your specific example:
var divs = $(".a");
if ( divs.get(0).isEqualNode(divs.get(1)) ) alert("Same");
The DOM Level 3 Core Spec has all the details. Essentially this returns true of the two nodes have matching attributes, descendents, and the descendents' attributes.
There's a similar .isSameNode() that returns true only if both elements are the same node. In your example, these are not the same nodes, but they are equal nodes.
Problem
How can we compare two HTML elements whether they are identical or not ? I tried this thing but no luck ``` <div class="a"> Hi this is sachin tendulkar </div> <div class="a"> Hi this is sachin tendulkar </div> ``` And then on button click, I call a function check() ``` var divs = $(".a"); alert(divs.length); // Shows 2 here which is correct if (divs.get(0) == divs.get(1)) alert("Same"); ``` But this is not working. Everything is same in two divs. Apart from this How can we compare whether two HTML elements are completely idential or not. Including their innerHTML, className, Id, and their attributes. Is this doable ? Actually, I have two HTML documents and I want to remove the identical content from both of them So two elements can have same id. PS: Updating after Crowder's valuable comments. If we compare two elements as strings, we would not get a match as their order of attributes may vary So the only option is to iterate through each child attribute and match. I still have to figure out completely working implementation strategy.