When I select a button to filter a set of divs (when it's active), I want to to be a certain color. This code works locally, but when I paste it into my website, multiple buttons start displaying as active. The rest of the code still works, but multiple buttons are colored at the same time. How do I fix this?
HTML
<div class="filter_container" id="myBtnContainer">
<button class="btn active" onclick="filterSelection('all')" type="button"> Show all</button>
<button class="btn cme_ce" onclick="filterSelection('cme_ce')" type="button"> CME/CE Session</button>
<button class="btn industry" onclick="filterSelection('industry')" type="button"> Industry-Sponsored (non-CME/CE)</button>
<button class="btn special" onclick="filterSelection('special')" type="button"> Special Event</button>
</div>
Javascript
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {element.className += " " + arr2[i];}
}
}
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function(){
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}