Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery only click once

I'm trying to disable a li click event after it has clicked the first time. Essentially to stop the array data being doubled. The click is working fine for each time. My current method doesn't appear to be working. I also need to disable the other li's from being clicked once the first one has :)

Thanks

JS code is:

$('#eventType ul li').click(function(e) {
    e.preventDefault();
    var value = $(this).attr('value');
    answers.push(value);
    // Below isn't working
    $(this).click(function() {
        return false;
    });
    console.log(answers);
});
like image 600
user72982708 Avatar asked Jan 07 '23 00:01

user72982708


1 Answers

you need to use one:

$('#eventType ul li').one('click',function(){
    //your code here
});

this event will be fired only once

UPDATE

you can do that using $.off()

$('#eventType ul li').one('click',function(){
  //your code here
  $('#eventType ul li').off('click');
});
like image 148
Amin Jafari Avatar answered Jan 08 '23 13:01

Amin Jafari