JavaScript JavaScript and the DOM Getting a Handle on the DOM Selecting by Id

Jason Ngo
Jason Ngo
6,918 Points

Why am I getting a typeError?

Why am I getting a typeError of undefined?

js/app.js
let button = document.getElementbyId('sayPhrase');
let input;

button.addEventListener('click', () => {
  alert(input.value);
});
index.html
<!DOCTYPE html>
<html>
  <head>
    <title>Phrase Sayer</title>
  </head>
  <body>
    <p><input type="text" id="phraseText"></p>
    <p><button id="sayPhrase">Say Phrase</button></p>
    <script src="js/app.js"></script>
  </body>
</html>

3 Answers

Colton Ehrman
Colton Ehrman
241 Points

You have an undefined variable declaration let input; and then you are trying to call alert(input.value)

Jason Ngo
Jason Ngo
6,918 Points

Thanks, I was able to figure it out!