ILovephp123 ILovephp123 - 7 months ago 25
HTML Question

Javascript Visible Element returning undefined

The HTML code that I'm trying to click is

<button type="submit" class="flatbutton">Send</button>


var i;
var button = document.getElementsByClassName("flatbutton");
for (i = 0; i < button.length; ++i) {
if (button[i].type == "submit") {
console.log(button[i].innertext);
}
}


For some reason, everything that is returned is undefined. This doesn't make sense, it should be returning "Send".

I'd prefer to only use Javascript and not JQuery

Answer

Simple typo in your code, innertext should be innerText

var i;
var button = document.getElementsByClassName("flatbutton");
for (i = 0; i < button.length; ++i) {
  if (button[i].type == "submit") {
    console.log(button[i].innerText);
  }
}
<button type="submit" class="flatbutton">Send</button>