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

Do i use 'const' or sayPhrase = document.getElementbyID('sayPhrase')

the video didnt quite help me for this challenge

js/app.js
var button;
var 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>

1 Answer

Torben Korb
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Torben Korb
Front End Web Development Techdegree Graduate 91,433 Points

Hi Jacquelyn,

first of all you can define variables with var or const. The latter is a newer syntax and doesn't allow to re-assign this variable in your code, which can be helpful and saves from some errors. But still you can decide which to use and both should work.

Understand what you put directly behind the var or const and in front of the equal sign is the variable name, this is what you decide to name it in your program. After the equal sign is what you assign to this variable name. In your case you need to assign a DOM element each to your variables. This what you correctly assumed in your headline.

So to correctly make your code work you would like to do something like this in your app.js:

const button = document.getElementById('sayPhrase');
const input = document.getElementById('phraseText');

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

Hope this helps. Happy Coding!