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

Yu Ito
Yu Ito
12,711 Points

I'm stuck with the task. I don't understand the question.

I have no idea what to code.

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.

js/app.js
let button;
let input;
let sayPhrase = document.getElementByID("sayPhrase");

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">id = Say Phrase</button></p>
    <script src="js/app.js"></script>
  </body>
</html>

Edit: Added the question itself so that others could better understand what is needed to be answered. - Dane E. Parchment Jr. (Moderator)

3 Answers

The instructions state: "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.." see in the challenge that there is already a variable called button declared in the code? You have to assign the code you've written to that button variable. Also the DOM method getElementById() is spelled with a lowercase d. hope this helps.

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

I really recommend you go back and watch the video carefully, as it explains the code necessary to accomplish the task.

If you do not want to go back and watch the video here is the the method that you want to use in order to get the element in the DOM through it's ID:

variableName = document.getElementById("Id Here");

In practice it will look like this (not the code to specifically answer the question, just an example, use it to answer the question):

//Assume we have an html tag with the ide of test
var element = document.getElementById("test");

Hopefully this helps.

he had already used the right method (with the exeption that the d shouldn't be capitalized), He just didn't assign it to the right variable.

Yu Ito
Yu Ito
12,711 Points

Thank you very much Ella and Dane for a quick response! You helped me a lot. I did It :D