I am working on creating a filter and I could use some assistance with making minimal changes to my code. I am confident that there is a practical solution for what I am trying to achieve and would appreciate any corrections or suggestions.
Button01's function is to hide/show all elements with the class name filter01,
Button02's function is to hide/show all elements with the class name filter02,
Both buttons have control over ElementX.
My goal is to have ElementX remain hidden until the button is clicked again, even if the other button is clicked in between.
HTML:
<div class="button01" onclick="filter01()">Filter 01</div>
<div class="button02" onclick="filter02()">Filter 02</div>
<div class="filter01 filter02">ElementX</div></a>
JS:
function filter01() {
var i;
var filter = document.getElementsByClassName("filter01");
for (i=0; i<filter.length; i++) {
if (filter[i].style.display) {filter[i].style.display = null; button01.style.color = null;}
else {filter[i].style.display = "none"; button01.style.color = "rgb(200,200,200)}
}
}
function filter02() {
var i;
var filter = document.getElementsByClassName("filter02");
for (i=0; i<filter.length; i++) {
if (filter[i].style.display) {filter[i].style.display = null; button02.style.color = null;}
else {filter[i].style.display = "none"; button.style.color = "rgb(200,200,200)}
}
}