I am trying to use buttons to filter a list of items on a page.
While one of the buttons is pressed, all the others will be disabled, and then, if I press another, then only this will be turned on, and the rest will be disabled.
I can’t figure out how to switch it, so if the button is already active, it will turn off all buttons, including itself.
Js fiddle
HTML:
<div class="calendar-filter">
<a href="#" class="dnco calendar-color-key btn btn-raised"><i class="material-icons"></i>Duty NCO</a>
<a href="#" class="main calendar-color-key btn btn-raised"><i class="material-icons"></i>Main Cadets</a>
<a href="#" class="wmt calendar-color-key btn btn-raised"><i class="material-icons"></i>Wing Marching Team</a>
<a href="#" class="juniors calendar-color-key btn btn-raised"><i class="material-icons"></i>Juniors</a>
<a href="#" class="exercise calendar-color-key btn btn-raised"><i class="material-icons"></i>Exercise</a>
<a href="#" class="other calendar-color-key btn btn-raised"><i class="material-icons"></i>Other</a>
</div>
JS:
function filterButtonClick(buttonClass) {
$('.calendar-color-key').each(function() {
$(this).addClass('disabled');
if($(this).hasClass(buttonClass)) {
$(this).removeClass('disabled');
}
});
}
$('.calendar-color-key').on('click', function() {
var filterButtonClasses = this.classList;
filterButtonClick(filterButtonClasses[0]);
});
source
share