sam999
sam999

Reputation: 115

Jquery off('click') on('click') bind and unbind

I have some buttons (Datatable button to export data) I need to prevent direct data download for that I am implementing OTP so when user first click on button its hows user a dialog box where he/she need to put OTP then if OTP matches then I need to remove .off("click"); method so that buttons can work again. on document ready I add this event like below

$(".dt-buttons button").each(function(){
    $(this).off("click");
});

Now how can i remove this .off("click"); so buttons can work again like default

Upvotes: 2

Views: 631

Answers (2)

Hopefully this will attach the normal behavior again on your element

             $("Your_selectors").on('click', function(){
                    $(this).trigger('click');
                });

Upvotes: 1

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

I suggest the use of common class with event delegation on() instead of detaching/attaching the event every time, you could give your button a common class example click_event and remove/add class as you want like :

$(".dt-buttons button").each(function(){
    $(this).removeClass("click_event");
});

//When you want to attach the event 
$('your_selector').addClass("click_event"); 

Upvotes: 2

Related Questions