0

I want to tget an element from the DOM with the class "TextInput-fragment". There is only one on the page. I can find it by class name, getting a collection. But I cant access it through indexing.

let x = document.getElementsByClassName("TextInput-fragment");
console.log(x);
console.log(x[0]);

dsad

In the first line we clearly see the element at index 0. But accessing it through x[0] gives me an undefined. x.length returns 0. How to get this element?

SG Tech Edge
  • 477
  • 4
  • 16
  • 1
    This HTMLCollection is live, at the time you try to get the element at index 0, the element certainly isn't in the document yet, when you check your console though, it will be, and the live view of the console will show you the element inside. So for a fix, wait that your element has been parsed before calling your script. – Kaiido Jul 22 '20 at 08:54
  • You can tell that @Kaiido is correct in this case because the collapsed view of the collection shows `[]` next to it -- it was empty when logged. It only got the element later. – T.J. Crowder Jul 22 '20 at 08:55

0 Answers0