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

I'm having trouble answering this question from the video series titled "Javascript and the DOM."

Here's the link for the question from the challenge: https://teamtreehouse.com/library/javascript-and-the-dom-3/getting-a-handle-on-the-dom/select-by-id

This is the question: "Declare a variable named button. Assign the variable the <button> element with the ID btn-phrase. Use the method that returns an element matching the specified ID."

// Complete the challenge by writing JavaScript below

let button = button.getElementById();

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

My answer, as you can see above is "let button = button.getElementById();" However, the website keeps telling me button is not defined when I have clearly defined it. What am I doing wrong?

Saim Diaz
Saim Diaz
10,095 Points

I am also having trouble with this quiz. Can someone help me with the answer?

3 Answers

My method produced a correct answer message from the website. Perhaps you're referring to a different question.

Try passing the id btn-phrase to button.getElementById().

Unfortunately, that didn't work.

Never mind. I figured it out.

Saim: The correct answer is let listItems = document.getElementsByTagName('li');. I've discovered using "const" instead of "let" works as well.

Saim Diaz
Saim Diaz
10,095 Points

Thank you for responding Luis. However, aren't we supposed to declare a variable with the name button? And aren't we supposed to use a method that returns an ID? On mine I have: const button = document.getElementByID('btn-phrase'); but it says that document.getElementByID isn't a function. Not sure what I'm doing wrong.

Try changing document.getElementByID to document.getElementById with a lowercase d in Id.

Saim Diaz
Saim Diaz
10,095 Points

Thank you jb30! That's exactly what was wrong. Appreciate the help. And no worries Luis, I think we were talking about a different question as well. Thanks for the help anyways!