2

I'm tring to change some element when the condition is satisfied. I need xTick[0] but it always undefined even there is HTMLColletion

var xTick = document.getElementsByClassName('some class name')

console logs

console.log(xTick)
output:
    HTMLCollection []
    > 0: text
    > 1: text
    > 2: text
    ...
    > 8: text
    > 9: text
      length: 10
    > __proto__: HTMLCollection


console.log(xTick.length)
output: 0


console.log(xTick[0])
output: undefined

console.log(xTick) has correct output but console.log(xTick[0]) and console.log(xTick.length) dosen't

jej1591
  • 21
  • 3
  • 2
    The list is updating live. Likely the element does *not* exist when you actually query for it, which `xTick.length` confirms. The element appears sometime later, and you see the object update live in the console too. – deceze Jul 22 '19 at 06:15
  • @deceze Oh, I thought element always exist. I made button and event that has ```console.log``` when I press button when element exist, I could get correct output. Thanks – jej1591 Jul 22 '19 at 06:30

0 Answers0