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 trialShung Chen
6,526 Pointswe can manipulate the button by its ID directly without selecting it first?
as title. wondering why we don't need this line
const myHeading = document.getElementById('myHeading');
const turnRed = document.getElementById('red');
const colorValue = document.getElementById('myTextInput');
//const changeButton = document.getElementById('changeButton'); // we don't need this line
changeButton.addEventListener('click', ()=>{
myHeading.style.color = colorValue.value;
});
1 Answer
Steven Parker
231,268 PointsThe video does select the element this way, but in the video the button's ID is "MyButton".
If you're wondering why this might work in your own test code, it would be because your browser is internally creating variables for elements using their ID as a variable name. But this behavior should not be relied on. It might not be done in all types of browsers; and even when it is, it might stop working in future versions.