Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
selecting the button with getElementById works, but using querySelector is returning null.
When I assign the 'btn-main' to a variable using
const btnMain = document.getElementById('btn-main');
Everything works as expected. However using
const btnMain = document.querySelector('.btn-main');
returns null. How should I properly using querySelector to select the button?
Jason Larson7,734 Points
You are probably using querySelector correctly, but you have the wrong value. You have
'.btn-main', which would be correct if you were selecting something with a class of 'btn-main'. Values in the querySelector method are like CSS definitions. Since you are trying to select an item with an ID of 'btn-main', you want to use:
const btnMain = document.querySelector('#btn-main');
Note that you don't have to make this distinction with
getElementById because you are already explicitly saying that you are going to be using the ID, so there's no need to make a special differentiation in the parameter.
Erik Biebinger6,658 Points
I had this issue too, because I did not update my html correctly. I am not sure if Guil is mentioning the change of the button id into a class. If so, sorry, my mistake.