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

Seth Johnson
Seth Johnson
13,361 Points

Not at all clear on what the first part of this 'challenge task' is asking me.

I'm completely stumped here. I'm not sure if I'm just not understanding the phrasing of what I'm being asked, but the first part of this 'challenge task' has every appearance of having nothing to do with anything covered in the previous video. Please help!

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>

1 Answer

Elijah Quesada
Elijah Quesada
Front End Web Development Techdegree Graduate 31,100 Points

Ask yourself this question, how would I reference the button element? Does the button contain any attributes I could use to reference? Well I see it has an id attribute and that's a start. I could reference that button by the id="sayPhrase".

You would then use the document.getElementById method.

 let  button = document.getElementById("sayPhrase");
Seth Johnson
Seth Johnson
13,361 Points

Thanks Elijah! This helped get me through it. The let keyword(s) were really what were confusing me, as well as not having the variable (which I thought was supposed to be a const ?!?) representing the button have the same name as its ID, which I thought was supposed to have been the preferred practice whenever possible.

Elijah Quesada
Elijah Quesada
Front End Web Development Techdegree Graduate 31,100 Points

No worries, glad it helped you out. Yes, I do believe as a best practice, you would start off a variable as const, and if need be, change it to let if it is going to be reassigned.