JavaScript JavaScript and the DOM Getting a Handle on the DOM Selecting by Id

Randell Purington
Randell Purington
9,992 Points

I could use so help, please

I looked this question up in the community and I used the help from the answers provided but I can not seem to get it correct.

the code I thought would work is the code below. This isn't correct so I am lost. let button = document.getElementById("sayPhrase"); console.log(button);

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>

2 Answers

Antonio Pala
Antonio Pala
Front End Web Development Techdegree Student 9,510 Points

Hi Randell, the challenge just wants you to use the button and input variables to select an element by its ID. So for example, in the first part of the challenge, it is looking for the following:

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

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

Based on this, try to solve the last part of the challenge!

Randell Purington
Randell Purington
9,992 Points

Thank you for the response but I am not understanding it even with your help, going back and reviewing the videos and my notes. I am not sure if there is a bug or if I am missing something to cause a syntax error.

Antonio Pala
Antonio Pala
Front End Web Development Techdegree Student 9,510 Points

What error are you getting, and were you able to complete the first part of the challenge?

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

let input;

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