-2

So I'm trying to get a button element from the page by the class name. But this function, as well as queryselector return undefined value. What might be the reason? Can it be because this element is a child of another element?

var checkout_btn = document.getElementsByClassName('button-submit');
console.log("INITIALIZED"); 
console.log(checkout_btn);

Console

Button html

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • use queries to select, `var btn = document.querySelector(".button-submit");` – Tudor Alexandru Dec 26 '20 at 18:18
  • 1
    The code you posted is OK, it isn't the problem. We can't help because we don't have enough information. If I have to guess, just stay sure you're executing it once the DOM is loaded. You can also put it at the end of the page, just before `

    ` tag.

    – kosmos Dec 26 '20 at 18:24
  • @TudorAlexandru Tried it. Same result. – Michael Michael Dec 26 '20 at 22:38
  • @kosmos I just put it in window.onload function. Same thing happens. Also I'm doing it through a content script for chrome extension, maybe it's the reason? Another weird thing is that if I find elements by button tag it finds the element I need, but when I access it with a subscript it says null. – Michael Michael Dec 26 '20 at 22:41

1 Answers1

-1

create and use the ID instead of className or try this document.getElementsByClassName("button-submit")[0]

p.s. Because getElementsByClassName returns an array-like object of all child elements which have all of the given class names.