Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Getting a Handle on the DOM Selecting by Id

What did I miss?

I get stuck with javascript

js/app.js
var button;
var input;

button.addEventListener('click', () => {
  alert(input.value);
}
let input = document.getElementById("phraseText");
let button = document.getElementById("sayPhrase");
console.log (button);
});
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>

2 Answers

Steven Parker
Steven Parker
220,937 Points

It looks like you have two issues:

  • "button" and "input" are already declared on the top two lines, so don't use "let"
  • you must assign the values before the call to "addEventListener"

I don't understand.

Steven Parker
Steven Parker
220,937 Points

Both the "var" and "let" keywords declare variables, so if you say "var button" and then later "let button ...." that's declaring it twice. But you can have "var button" at the top and later assign it "button = ...".

And the code that calls "button.addEventListener..." depends on "button" already having a value, so the assignment must be done before that line instead of after it.

Cheo R
Cheo R
37,150 Points

Since the variables input and button are already declared, you don't need to use let.

Remove the last two lines.