I want to iterate through all HTML elements with a specific class name in TypeScript. I tried:
let elements = [...document.getElementsByClassName("myclass")];
But I get this error TS2488: Type 'HTMLCollectionOf ' must have a '[Symbol.iterator]()' method that returns an iterator.
let elements = document.getElementsByClassName("myclass");
Array.prototype.forEach.call(elements, function (item:HTMLElement) {
// do stuff with the item
});
but this only works for the first item. If I use alert(item.innerText)
I only receive one alert.
What's the proper way to iterate through a HTMLCollection
?
Thank you!