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

Shadrach Thomas
PLUS
Shadrach Thomas
Courses Plus Student 8,915 Points

please i need help here

please i need a very good explanation to how to get this right cause i don't think i'm doing anything wrong here

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>

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Shadrach,

You're setting the reference correctly, but you still have a semicolon after the variable name. In JavaScript, a semicolon ends a statement, so the reference is not being assigned to the variable. To assign a variable, you use the equal (=) sign.

So, you just need to delete the semicolon and put in an equal sign.

Keep Coding! :dizzy: :)

Hi,

You are not properly assigning the values to your variables. You js does not know yet what 'button' or 'input' are supposed to be. Right now they are just variables with no values assigned. The correct code would be something like this

//what you have
//let button; documment.getElementById('sayPhrase');

//what it should be -> you get the button and assign it to the constant button  
let button = document.getElementById('sayPhrase');

//what you have
//let input;

//what it should be -> right now input is just a constant, but you need to tell js what input should refer to
let input = document.getElementById('phraseText');

//now your button refers to your input button with id sayPhrase
//now your input refers to the input text box with id phraseText

button.addEventListener('click', () => {
  alert(input.value);
});