Answer the question
In order to leave comments, you need to log in
How to make a single button radio button in jQuery?
There is one button. It is necessary that on one click one function was performed, and on repeated - another function was performed. How to implement*?
Answer the question
In order to leave comments, you need to log in
Easy option
var i = 0;
$('button').click(function() {
i++;
if (i == 1) {
// function 1
} else {
// function 2
i = 0;
}
});
Create a variable and check its value when the button is clicked. Depending on the value of the variable, perform some action and change the value of the variable.
For example https://jsfiddle.net/r2uwdL30/
you 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));
$('#test').clickToggle(function() {
// Функция 1
},
function() {
// Функция 2
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question