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 trialKim Dallas
11,461 PointsWhat did I miss?
I get stuck with javascript
var button;
var input;
button.addEventListener('click', () => {
alert(input.value);
}
let input = document.getElementById("phraseText");
let button = document.getElementById("sayPhrase");
console.log (button);
});
<!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
231,236 PointsIt 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"
Cheo R
37,150 PointsSince the variables input and button are already declared, you don't need to use let.
Remove the last two lines.
Kim Dallas
11,461 PointsKim Dallas
11,461 PointsI don't understand.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsBoth 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.