I'm working on a SVG file, and I'd like to count how many polygons have a certain class (lets say ".red")
Here's the simple javascript loop:
var polygons = document.getElementsByTagName("polygon");
var j = 0;
for (i = 0; i < polygons.length; i++) {
if (polygons[i].className == "red") {
j++;
}
};
alert("There are " + j + " red shapes")
and the fiddle : https://jsfiddle.net/tuj0rmnx
I can't get the expected result. 0 is returned instead of 3... Can't figure out why... I guess there must be a DOM issue or someting, but can't get it right...
I have read this : How to access SVG elements with Javascript but I don't want to create an event. All I want to achieve at the end, is to "print" the result automatically in the page using document.write() method...
Any help appreciated!!