rob.m rob.m - 1 year ago 55
Javascript Question

How to save a list of classes using javascript to compare them against other classes later?

I have a series of polygons :

<path class="_1600 _1500 _1400 leaflet"></path>
<path class="_1300 _1700 _1900 leaflet"></path>
<path class="_1600 _1400 _1800 leaflet"></path>

And buttons:

<button class="_1600 _1500"></button>
<button class="_1600 _1300 _1200"></button>
<button class="_1300 _1200 _1700 _1800"></button>
<button class="_1300 _1200 _1100 _1900"></button>

I need to assign a class to it on a click and this is how I do it and works fine:

On click:


Now the polygons become:

<path class="_1600 _1500 _1400 leaflet"></path>
<path class="active _1300 _1700 _1900 leaflet"></path>
<path class="_1600 _1400 _1800 leaflet"></path>

How can I save the list of classes of the clicked element (to which I also have added
after a click)?
I would like to save them so that I can later comprare this list for any match with another list of classes and if any match, attach a
class to the matching elements.

I must use:

function onEachFeature(feature, layer) {
click: panelShow

function panelShow(e) {

Answer Source

You can use className to get a space-separated string containing all the classes.


If you want to have something more fancy, like a static classList copy, you can always create a dummy element:

function staticClassListCopy(element) {
  var dummy = document.createElement('div');
  dummy.className = element.className; // import classes
  return dummy.classList;

This object will be an array-like, so you can iterate it easily to do the desired comparisons.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download