0

I just want to know how to enable click in jquery. I disabled click on element using .off() but don't know how to enable it again.

I tried to use the code here, but didn't work or maybe I put it in a wrong way.

I made a simple app that disables the click when the countdown is 3 and below and enable again when the counter resets.

Hope you help me.

Thanks.

var counter = 10;
  
setInterval(function(e){
  $('.counter').text(counter);
  counter --;
  if (counter <= 3) {
    $('button').off('click');
  }
  if(counter < 0){
    counter = 10;
    // enable click here
    var myFunc = function(event){
     event.stopPropagation();
     // execute a bunch of action to preform
}

$('button').on('click', myFunc); //bind myFunc
    
  }
}, 1000);

var clicks = 1;
$('button').click(function(){
  $('.clicks').text(clicks++);
});
.clicks{
  background-color: #DDD;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="counter"></div>
<br>
<button>click</button>
<div class="clicks">
  
</div>
user123
  • 435
  • 1
  • 6
  • 23
  • thats the link I add sir – user123 Jul 26 '18 at 08:59
  • https://stackoverflow.com/questions/14808351/disable-then-re-enable-click-function-jquery?lq=1 – Gyepesto Jul 26 '18 at 09:00
  • doesn't work too – user123 Jul 26 '18 at 09:03
  • 1
    @Mark: this question is closed as a duplicate, but your problem is not the usage of `on` or `off`, but that the `clicks++` is in a function that you then remove using `off`. Move the code from the click handler at the end of your example int `var myFunc = function(event){ ...` and you should be good to go. – FatalMerlin Jul 27 '18 at 04:09

0 Answers0