Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was looking for a way to see if two diffrent HTML elements could be compared to see if they have the same result.

eg.
JavaScript
compare("<span class="class1"></span>", "<span class="class2"></span>")
would return true. But
JavaScript
compare("<b class="class1"></b>", "<b>class="class2"></b>")
would return false.

NOTE: Pretend that class1 and class2 have no CSS code.

Is there any API that can do that for me?

What I have tried:

I really don't know where to start, I tried Googling how but I couldn't find anything that suited my needs. So, any help would be appreciated.
Posted
Updated 30-Sep-20 0:40am

There could be multiple ways, one of them be with the use of: Node.isEqualNode()

Reference: Node.isEqualNode() - Web APIs | MDN[^]

Quote:
The Node.isEqualNode() method tests whether two nodes are equal. Two nodes are equal when they have the same type, defining characteristics (for elements, this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that must match varies depending on the types of the nodes.


Example usage:
JavaScript
var isEqualNode = node.isEqualNode(otherNode);
 
Share this answer
 
If you're using the class just as a flag to compare two elements, why not use something just for that purpose? You didn't say how you selected the two element to start with - so I'll assume you gave them each an "id" - which are always different or your page will be messed up.
HTML
E.g.:
<element1 myflag='something'      id='e1'>
<element2 myflag='also-something' id='e2'>

Now you can use:
document.getElementById('e1').getAttribute('myflag') to retrieve the attributes for the elements:  Just compare the strings!


This makes two assumptions, top paragragph, and if either is wrong you'll need to adapt (possibly making them true?). Don't forget: string comparisons are case sensitive unless you take that into account.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900