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 Getting a Handle on the DOM Select by ID

Kelsey Donegan
Kelsey Donegan
3,342 Points

Is step 1 not exactly the same as step 2?

Step one asks me to declare a variable called button, the get the variable to point to the element called btn-phrase. Step two asks me to do the same thing but with a variable called input and the element input-phrase. This is the code I've written and it's not working. Step 1 worked fine, but now step 2 says it's wrong.

const button = document.getElementById ('btn-phrase');
const input= document.getElementById ('input-phrase');
app.js
// Complete the challenge by writing JavaScript below
const button = document.getElementById ('btn-phrase');
const input = document.getElementById ('input-phrase');

button.addEventListener('click', () => {
  alert(input.value);
});
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Phrase Sayer</title>
  </head>
  <body>
    <p><label for="input-phrase">Type a phrase</label></p>
    <p><input type="text" id="input-phrase"></p>
    <p><button id="btn-phrase">Say Phrase</button></p>
    <script src="app.js"></script>
  </body>
</html>

1 Answer

Your code looks fine. You can pass the challenge by removing the space between document.getElementById and ('input-phrase');, but I don't see any reason why it would allow a space in the first line and not in the second.

const button = document.getElementById ('btn-phrase');
const input= document.getElementById('input-phrase');
Kelsey Donegan
Kelsey Donegan
3,342 Points

That's so weird. I'm not sure why it allowed it on the first line but you're right, it worked perfectly. Thank you!