I have some elements in my HTML with class node-item
, I access them in my component using:
let nodeItems = document.getElementsByClassName('node-item');
and when I log nodeItems
it gives me a HTMLCollection[]
with length 4.
I tried many ways but still can't iterate on nodeItems
:
1- first try:
let bar = [].slice.call(nodeItems);
for (var g of bar){
console.log(g); //gives me nothing
}
2- second try:
for(let c of <any>nodeItems) {
console.log(c); //gives me nothing
}
And I tried array iteration and object iteration but still undefined
or error
. also tried:
let nodeItems = document.querySelector(selectors);
But same problems.
Array.from()
would be cleaner than[].slice.call()
). Are you sure the collection does contain the elements? Remember that it is a live node list. Please doconsole.log(nodeItems.length)
. – KaciekacynodeItems
and it's give me a full HTMLCollection but when log the length it gives me 0 . – Cordesfor of
compiles to iterators or array-index-counting, it should work. If logging the length gives0
, as per the OPs comment, then the problem is with using the DOM at the wrong time. Could you please re-close it? – Kaciekacyfor..of
because it compiles tofor
but there will be compilation error (<any>nodeItems is ugly workaround for that). Can you list the dupes you've marked? I didn't expect it to be instantly reopened. – Conwayconsole.log
confusion on mutated arrays – Kaciekacy