Managing click on-off on a div

Managing click on-off on a div
0

#1

Hi there,

I wrote this code :

$('td').on('click', function () 
{
    tdClicked(this);
});

function tdClicked(obj)
{
	alert(obj.id);
}

Somewhere in the code I did:
 $('td').off('click');

Now I’m stuck at a point on how to switch it to enable clicking. Any help?


#2

What is your goal? This will help answer your question.

The off() method removes an event handler from an element. See http://api.jquery.com/off/ for documentation on this method. Because you are adding an event handler and then removing one, nothing would happen unless you could click on the element before the browser reads the script file.

Now if you wanted to add an event handler to an element, you would listen for click on the document, and add the event listener to the event target, this would add and remove the event at the same time according to the code you wrote. So it’s important you explain your intentions because I am not even satisfied with this answer, it is all I can infer.


#3

#4

So… What’s the question? You want clicking the element to remove the click listener? Or you want to add more event listeners while ensuring there’s no more than one click listener at any one time? Or you just want to add a single listener and you can’t get it to work? It would also help if you posted a live version of your app.


#5

The question was that, I wanted to add more event listeners while ensuring there’s no more than one click listener at any one time. So, to once it was switched off, i was unable to find out as to how can I bring it back up with mutiple methods having the onclick thingy on. So then finally I found a way to do that by:

$('td').off('click').on('click', function () 
{
    tdClicked(this);
});

So that there are not more than two ‘on-clicks’ at one point of time. Am I doing something wrong, because my application started working after using this.
Pen Here


#6

I’m having trouble understanding why you’d want to do this?


#7

Yes, this is exactly correct if you want to remove any and all existing click handlers and then add a new one.