I am a beginner and I just do not know what to do. I tried various things like function click, getElement and onclick etc. but I just don’t know why it’s not working and what I should do in the first place. All I ever find is the button color and background color but nothing on text color and list. I just couldn’t find a proper solution anywhere :/
I have a list (it’s two seperate ones because of how I wrote the CSS lol) of rainbow colors written out and their respective colors (there are more but I didn’t want the post to be too long because of all the colors). I wanted all of them to turn black at once when you click on a button.
The files are all seperate.
it looks somewhat like this:
.list {
margin: 0 50px 0 50px;
}
#red {
list-style-type: circle;
color: red;
}
#orange {
list-style-type: circle;
color: orange;
}
#yellow {
list-style-type: circle;
color:yellow;
}
#brown {
color: brown;
}
#grey {
color: grey;
}
<div class="list">
<ul>
<li id="red"> red </li>
<li id="orange"> orange </li>
<li id="yellow"> yellow </li>
</ul>
</div>
<div class=list2>
<ol start=3>
<li id="brown"> brown </li>
<li id="grey"> grey</li>
</ol>
</div>
<button id="black">Click me</button>
2
Answers
Adding an event listener to the button will allow you to fire a method whenever it is clicked. Once you have a method firing on click, you can set up a method to pull all the list elements to updated, loop through and add a class to set the color to black.
I’ve made the changes for you below. Note that I’ve updated your ids to classes, as it is typically best to control styles by class rather than id, as classes can be reused, while ids should only be assigned to a single element. I also added a new
black
class which set the color to black. Finally I added a common class to all list elements to allow pulling them specifically.Use "document.querySelectorAll();" for list tag and just iterate through it while changing the color