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 trial
shirapatuladhar
3,102 PointsChange color for an individual list element.
How can we select individual elements ? The code above doesn't seem to work. Can someone guide me as to what can be done to select li's and change their color when we click on them.
const mylist = document.getElementsByTagName('li');
for (i=0;i < mylist.length; i+=1){
console.log(mylist);
mylist[i].addEventListener('click',function(){
mylist[i].style.color = 'green';
});
2 Answers
KRIS NIKOLAISEN
54,974 PointsChange mylist[i] to this and you should be good to go.
const mylist = document.getElementsByTagName('li');
for (i=0;i < mylist.length; i+=1){
console.log(mylist[i]);
mylist[i].addEventListener('click',function(){
this.style.color = 'green';})
};
shirapatuladhar
3,102 PointsThat works , but isn't it the same thing ?