Reputation: 3558
I have a .on('click','td', function(){})
event.
I turn it .off('click','td')
. I now would like to .on()
the same event again.
I tried some combinations of .on()
and it did not work. Could be done by assigning it to a variable, not sure how to do that though.
Upvotes: 3
Views: 6098
Reputation: 4912
Put the event handler function in a variable, and use it like this:
var handler = function(e){
//code here
}
//on
$('#myselector').on('click', handler);
//off
//Try this, this will only turn 'handler' off
$('#myselector').off('click', handler);
//If the above doesn't work, then try this
//This will turn off all your other click handlers for the same element,
//if for some reason turning off a particular handler doesn't work!
$('#myselector').off('click');
//on again
$('#myselector').on('click', handler);
Upvotes: 8
Reputation: 3641
I am assuming that clicking the TD try this: first having your td a class to tell if it is on or off. By default TD will have td-off class, like this
<td class="td-off">
then set your an event changing the class.
.on('click','td',function(){
if($(this).hasClass('td-off')){
$(this).removeClass('td-off');
$(this).addClass('td-on');
}else{
$(this).removeClass('td-on');
$(this).addClass('td-off');
}
});
then finally set the event of your on or off
$('.td-on').click(function(){
//Your event on
});
$('.td-off').click(function(){
//Your event off
});
It may not be the best answer but I hope you get the idea
Upvotes: 4