Turn click events on and off

Turn click events on and off

taichtaich Posts: 2Questions: 1Answers: 0

I want to disable all click events from my datatable.
I can do this with

var table = $('#example').DataTable();
$('#example').off('click');

But I can not enable the eventhandler later on.
This does not work.

$('#example').on('click');

Same with .bind() and .unbind()
What am I doing wrong?

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Hi @taich ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • taichtaich Posts: 2Questions: 1Answers: 0

    Here is a test case.
    http://live.datatables.net/wufiwute/1/

    Once you click on any row, all data of the row you clicked is logged to console. After that the eventhandler is turned off(). And you cant turn it on any more.

    No more click events will happen.

  • kthorngrenkthorngren Posts: 20,313Questions: 26Answers: 4,771
    edited September 2019

    Thanks for the test case - it helps to clear up what events you are referring to. The problem is you can't just use $('#example').on(); . When you use jQuery .off() it does allow for an empty set of parameters with this comment in the docs:

    Calling .off() with no arguments removes all handlers attached to the elements.

    The docs for jQuery .on() specify that one or more events must be passed to the API. Otherwise jQuery won't know what events to turn on.

    Kevin

This discussion has been closed.