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 trialHenry Garmendia
Courses Plus Student 11,762 PointsChallenge Task 1 of 3 Select the button with the ID "makeItRed" and assign it to the button variable.
I don't understand why is not letting me select the ID with
let button = document.getElementById('makeItRed'); or let button = document.querySelector('makeitRed');
what am I doing wrong, can anyone please help?
const warning = document.getElementById("warning");
let button = document.querySelectorAll('button');
<!DOCTYPE html>
<html>
<head>
<title>Adding an Event Listener</title>
</head>
<link rel="stylesheet" href="style.css" />
<body>
<div id="warning">
Warning: My background should be red!
</div>
<button id="makeItRed">Make It Red!</button>
<script src="app.js"></script>
</body>
</html>
8 Answers
Umesh Ravji
42,386 Pointslet button = document.querySelectorAll('button');
That will return ALL of the elements that match the selector, all the buttons in this case.
let button = document.querySelector('button');
Returns the first match, which will give you the answer.
However, I recommend using..
let button = document.querySelector('#makeItRed');
.. because it lets you be way more specific, imagine if your page had a hundred buttons :)
let button = document.getElementById('makeItRed');
You mentioned that didn't work, but it does :)
Sebastian Velandia
24,676 Pointslet button = document.getElementById('makeItRed')
nicolabell
18,212 PointsI thought this was right too but it's not working for me!
Adnan Rruka
4,838 PointsDifficult to understand the question
Henry Garmendia
Courses Plus Student 11,762 Points@Umesh, I forgot to add the "#" Thank you for your help
Caleb Spindler
11,397 PointsEnded up here for the same reason as others:
let button = document.getElementById("makeItRed");
^ That should be a valid option, right? But it only worked when I used the query selector format (below):
let button = document.querySelector("#makeItRed");
Josh Mord
Full Stack JavaScript Techdegree Student 238 PointsNeither querySelector, querySelectorALL, getElementById, or getElementsByClassName work for me. Unless I'm missing something crucuial, I think this challenge is bugged (i'm using firefox).
Shiva Gautam
7,892 PointsI too went through the same condition but finally var button = document.querySelector('button'); worked for me. :)
Ndhokoyo Aaron
6,589 Pointsvar button = document.querySelector('button');
Larissa Ragland
6,653 Pointsvar warning = document.getElementById("warning"); let button = document.querySelector("#makeItRed");
nicolabell
18,212 Pointsnicolabell
18,212 PointsI also tried to use your last answer:
let button = document.getElementById('makeItRed');
But this wasn't working for me either, which is why I've ended up here.
Robert Rydlewski
3,828 PointsRobert Rydlewski
3,828 PointsYep...
let button = document.getElementById('makeItRed');
do not work for this challenge. However thanks for an explanation. Good job :)