T
T
tereshock982018-02-03 01:19:23
JavaScript
tereshock98, 2018-02-03 01:19:23

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

3 answer(s)
Y
y0u, 2018-02-03
@tereshock98

Easy option

var i = 0;

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

    if (i == 1) {
        // function 1
    } else {
        // function 2
        i = 0;
    }
});

V
VelAnna, 2018-02-03
@AnnaVel

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/

V
Vladimir Proskurin, 2018-02-03
@Vlad_IT

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));

then you use
$('#test').clickToggle(function() {   
    // Функция 1
},
function() {
    // Функция 2
});

I stole from here https://stackoverflow.com/questions/4911577/jquery...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question