0

good day I am new to javascript and I don't understand what line of code is this?

Source.

document.getElementsByTagName("INPUT")[0].setAttribute("type", "button"); 

I don't understand why is there ("INPUT")[0] <-----the [0] part on the code? can someone explain?

appreciate

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Ghost
  • 23
  • 3
  • 1
    `getElementsByTagName` returns a list of matched elements. `[0]` simply takes the first element in that list – Nikos M. May 03 '20 at 06:14
  • 1
    `document.getElementsByTagName("INPUT")` returns an array. Adding `[0]` references the first element of the array. – kmoser May 03 '20 at 06:14
  • 1
    FWIW `document.getElementsByTagName("INPUT")` is not an array, it's an `HTMLCollection`. `Array.isArray(document.getElementsByTagName("INPUT"))` => `false`. – Monte Roden May 03 '20 at 06:16
  • ok guys thank you :D . – Ghost May 03 '20 at 06:22

0 Answers0