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

code challenge selecting by id I am not quite sure what the instructions are asking in task 1.

Right now I have:

<code> 
let button;
let input;
const sayPhrase = document.getElementById('sayPhrase');

button.addEventListener('click', () => {
sayPhrase.value;
});
</code>

But I know something is missing or I am way off.

js/app.js
let button;
let input;
const sayPhrase = document.getElementById('sayPhrase');

button.addEventListener('click', () => {
  sayPhrase.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

Steven Parker
Steven Parker
231,008 Points

You have the right value, but put it in the wrong variable.

The instructions say, "There is a variable named button in app.js. Set its value to contain a reference to the button element in index.html with the ID of sayPhrase."

You got a reference to the correct button, but instead of putting it in button, you put it in a new variable named sayPhrase.

Steven Parker
Steven Parker
231,008 Points

Jason Anders — or maybe it's the time difference between just answering the question and writing an elegant tutorial. :white_flower: :wink:

thanks steven! i appreciate it!

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Michael,

You have the right syntax for selecting the id, but you aren't assigning it to the correct variable.

The challenge did not ask you to create the const sayPhrase ... as you have. It wants the result of the selection to be assigned to the button variable. So, you need to delete the variable you created and move the selector to the button.

Keep in mind for future challenges that the instructions are very specific and very picky. If you add something that wasn't asked for, deleted something that you weren't told to, or even forget a period in a string... the challenge will throw a Bummer!.

let button = document.getElementById('sayPhrase');;
let input;

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

Keep Coding! :dizzy:

thank you for your help!!