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

Seth Johnson
Seth Johnson
15,200 Points

Not at all clear on what the first part of this 'challenge task' is asking me.

I'm completely stumped here. I'm not sure if I'm just not understanding the phrasing of what I'm being asked, but the first part of this 'challenge task' has every appearance of having nothing to do with anything covered in the previous video. Please help!

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

1 Answer

Ask yourself this question, how would I reference the button element? Does the button contain any attributes I could use to reference? Well I see it has an id attribute and that's a start. I could reference that button by the id="sayPhrase".

You would then use the document.getElementById method.

 let  button = document.getElementById("sayPhrase");
Seth Johnson
Seth Johnson
15,200 Points

Thanks Elijah! This helped get me through it. The let keyword(s) were really what were confusing me, as well as not having the variable (which I thought was supposed to be a const ?!?) representing the button have the same name as its ID, which I thought was supposed to have been the preferred practice whenever possible.

No worries, glad it helped you out. Yes, I do believe as a best practice, you would start off a variable as const, and if need be, change it to let if it is going to be reassigned.