Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,271 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
231,271 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.