0
var list = document.getElementsByClassName("sidebar-holder");
console.log(list);// html collection of length 2
console.log(list[0]);// undefined

Here I have run the following code and I have used the comments to console.log() the results.

While the elements with that class name are being rendered asynchronously, I do not believe that this is the problem as the first console.log() renders the expected result. The second one does not.

hgb123
  • 13,869
  • 3
  • 20
  • 38
  • You're seeing a live reference of your list (ie: so you're seeing its contents at the time of viewing it), not its contents for when it was logged. – Nick Parsons Aug 03 '20 at 02:56

1 Answers1

0

Add Array.from(list) to convert the HTML element to array before you can iterate over it. That's so because HTML element are array-like but not pure arrays.

Fritzdultimate
  • 372
  • 2
  • 11