How to make the switch a button in JQuery?

There is one button. Need that one-click was performed one function, and re - executed another function. How to implement a*?
June 8th 19 at 16:40
3 answers
June 8th 19 at 16:42
Solution
Easiest option
var i = 0;

$('button').click(function() {
i++;

 if (i == 1) {
 // function 1
 } else {
 // function 2
 i = 0;
}
});
I need to add a class to the unit. Press once, the class is added, but re-clicking class is not removed.
$('.songs').click(function () {
 var i = 0;
i++;
 if (i == 1) {
$('.songs').addClass('button-active');
 } else {
$('.songs').removeClass('button-active');
 i = 0;
};
 });
- will.Windl commented on June 8th 19 at 16:45
, https://jsfiddle.net/o3cvu9y4/1/ - peggie_Aufderh commented on June 8th 19 at 16:48
Can , please write is the same, only not with the Alpine, and with the addition or removal of a class?

And anyway, is it possible to delete class from class? - will.Windl commented on June 8th 19 at 16:51
why you add i++;? In this case, the variable you will always be equal to 1. - citlalli65 commented on June 8th 19 at 16:54
And something to add to the variable is changed with each press? - will.Windl commented on June 8th 19 at 16:57
$('.songs').click(function() {
$('.songs').toggleClass('button-active');
});
- nikita.Stracke commented on June 8th 19 at 17:00
,

var i = 0;

$(document).on('click', '.songs', function() { 
 if (i == 1) {
$('.songs').addClass('button-active');
 i = 0;
 } else {
$('.songs').removeClass('button-active');
 i = 1;
};
});
- citlalli65 commented on June 8th 19 at 17:03
That woooooooooot, what you need! Thank you very much!!!! - will.Windl commented on June 8th 19 at 17:06
now , that means a poorly drafted description of the task. You only need to use toggleClassas written above, but you have in the task description does not say anything about classes. - mellie commented on June 8th 19 at 17:09
June 8th 19 at 16:44
Create a variable and when you click on the button to test its value. Depending on the value of a variable to perform some action and change the value of the variable.
For example https://jsfiddle.net/r2uwdL30/
June 8th 19 at 16:46
Write
(function($) {
 $.fn.clickToggle = function(func1, func2) {
 var funcs = [func1, func2];
 this.data('toggleclicked', 0);
 this.click(function() {
 var data = $(this).data();
 var tc = data.toggleclicked;
 $.proxy(funcs[tc], this)();
 data.toggleclicked = (tc + 1) % 2;
});
 return this;
};
}(jQuery));

then you use
$('#test').clickToggle(function() { 
 // Function 1
},
function() {
 // Function 2
});

From here stole https://stackoverflow.com/questions/4911577/jquery...

Find more questions by tags jQuery