最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - javascript button toggle only one at a time - Stack Overflow

matteradmin8PV0评论

I'm trying to use buttons to filter a list of elements in a page.

So far if one of the buttons is clicked all the others will be disabled and then if I click a different one then only that will be enabled and the others will be disabled.

I cannot work out how to toggle it so if the button was already active it would un-disable 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]);
});

I'm trying to use buttons to filter a list of elements in a page.

So far if one of the buttons is clicked all the others will be disabled and then if I click a different one then only that will be enabled and the others will be disabled.

I cannot work out how to toggle it so if the button was already active it would un-disable 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]);
});
Share Improve this question asked Jan 22, 2016 at 10:43 georgehartlettgeorgehartlett 431 silver badge5 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5
$(".calendar-color-key").click(function() { //select by class
  var clicked = $(this);

  if (clicked.hasClass('toggle')) {
    $('.calendar-color-key').removeClass('disabled'); //enable all again  
    clicked.removeClass('toggle');
  } else {
    $('.calendar-color-key').removeClass('toggle');
    clicked.addClass('toggle');
    clicked.removeClass('disabled');
    $('.calendar-color-key').not(clicked).addClass('disabled'); //disable everything except clicked element
  }
});

https://jsfiddle/6kx8ncdb/2/

Why not just use a class default class for your buttons like filter-button so when you want to disable them all just use:

$(".filter-button").addClass("disabled");

And use an active class it would be a big help so you can have

$(".active").on('click', function(){
    $(".filter-button").addClass("disabled");
    $(".active").removeClass("active");
});

UPDATE: don't forget to remove the active class when adding disabled I updated the code.

Post a comment

comment list (0)

  1. No comments so far