How to remove the previous class?

5a3529b143c86920114555.png

I have two buttons and one unit black color, when you click on the first button is written to the block class and it becomes yellow(Block). But when I click on the second button, this unit will add another class. Now, I want when clicking left of the previous class
June 10th 19 at 14:29
2 answers
June 10th 19 at 14:31
Before use addClass removeClass.
But if there are 20 buttons, the mess will be. And in another way? - Cortez commented on June 10th 19 at 14:34
I don't understand what the mess? You add and remove classes on one line. - nasir commented on June 10th 19 at 14:37
, 5a352db52cf9b406245548.png

here before each need ? - Cortez commented on June 10th 19 at 14:40
In this case, create a separate function that will delete the classes for the required items, and will be called every time at the beginning of your functions presented on the screen. - nasir commented on June 10th 19 at 14:43
Well, now try. Thank you - Cortez commented on June 10th 19 at 14:46
June 10th 19 at 14:33
(function() {
 var currentClass = null;

 $('input[type=button]').click(function() {
 var targetClass = $(this).attr('data-class');

 if (currentClass) {
$('#block').removeClass(currentClass);
 currentClass = null;
}

 $('#block').addClass(currentClass = targetClass);
});
})();
Thank you very much - Cortez commented on June 10th 19 at 14:36

Find more questions by tags jQuery