Tuesday, 10 July 2018

For loop for HTMLCollection elements

I'm trying to set get id of all elements in an HTMLCollection. I wrote the following code:

var list= document.getElementsByClassName("events");
console.log(list[0].id); //first console output
for (key in list){
    console.log(key.id); //second console output
}

But I got the follwoing output in console:

event1
undefined

which is not what I expected. Why is the second console output undefined but the first console output is event1?



from For loop for HTMLCollection elements

No comments:

Post a Comment