Easy: Addclass removeClass hasClass problem?

I have several buttons in a div container that need to be activated, but only one active button at a time.

EDIT:

html: div container .filter (btn, btn, btn, etc.) there are 3 button containers. The third container has a button with an identifier: # filter1-none

In the code below, there is only one button for each group.

In addition, how can I remove the active only from the button with the identifier # filter1-none ONLY if any of the other buttons is active?

$('.filter .btn').click(function () { $(this).addClass('active').siblings('.active').removeClass('active'); }); 
+4
source share
2 answers

Usually I do something like this:

 $('.filter .btn').click(function () { $(this).addClass('active').siblings('.active').removeClass('active'); }); 

EDIT:

This does not allow you to "switch" behavior. This way .btn will behave like a radio, not a flag. Let me know if you are looking for flag type behavior.

+1
source

Have you tried something like?

 $('.filter .btn').click(function () { $(this).closest('.filter').find('.btn.active').removeClass('active'); $(this).addClass('active'); }); 
+2
source

Source: https://habr.com/ru/post/1446096/


All Articles