JQuery - check if first click

I have 2 functions (A and B) called with one click div. I need to call function A only on the first click and function B whenever it is pressed. How can i do this?

+4
source share
3 answers

A simpler solution:

$('element').one('click',function(){
// Call A
}).click(function(){
// Call B
});
+6
source

You can use . one () for your first function and , as usual, for your second function .click()

function a() {
    console.log('Fire once!');
}

function b() {
    console.log('Always fired!');
}

$('span').one('click', function() {
    a();    
});

$('span').click(function() {
    b();    
});

Demo Screenshot

0
source

, , , off(), , .

$(function(){
    $("selector").on("click",FunctionA);
});

function FunctionA(){
    /*Do stuff*/
    $(this).off("click");
    $(this).on("click",FunctionB);
}
0

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


All Articles