tags:

views:

31416

answers:

8

who knows the answer?

i have a input type="image" which sort of acts like the little cell notes in excel. if someone enters a number into the textbox this input-image is paired with, i setup an event handler for the input-image and then when the user clicks the image they get a little popup to add some notes to the data.

the problem i'm having, is that when a user enters a zero into the textbox, i need to disable the input-image's event handler. i tried to do something like $('#myimage').click(function { return false; }); but that did not work.

+38  A: 

Look at unbind.

Phill Sacre
+7  A: 

maybe the unbind method will work for you

$("#myimage").unbind("click");
John Boker
+4  A: 

check out the documentation for unbind which you can see here http://docs.jquery.com/Events/unbind

YonahW
+6  A: 

This can be done by using the unbind function.

$('#myimage').unbind('click');

You can add multiple event handlers to the same object and event in jquery. This means adding a new one doesn't replace the old ones.

There are several strategies for changing event handlers, such as event namespaces. There are some pages about this in the online docs.

Look at this question (that's how I learned of unbind). There is some useful description of these strategies in the answers.

http://stackoverflow.com/questions/48931/how-to-read-bound-hover-callback-functions-in-jquery

Mnebuerquo
+64  A: 

In your example code you are simply adding another click event to the image, not overriding the previous one:

$('#myimage').click(function() { return false; }); // Adds another click event

Both click events will then get fired.

As people have said you can use unbind to remove all click events:

$('#myimage').unbind('click');

If you want to add a single event and then remove it (without removing any others that might have been added) then you can use event namespacing:

$('#myimage').bind('click.mynamespace', function() { /* Do stuff */ });

and to remove just your event:

$('#myimage').unbind('click.mynamespace');
samjudson
Is there a way to test if `unbind()` is working? I've added it and both events are still firing.
DavidYell
Well if both events are still firing then obviously its not working. You'd have to give more info to get a proper answer. Try asking a separate question.
samjudson
+2  A: 

@samjudson: As far as i can tell from the jquery-source, the syntax is "event.namespace", not "namespace.event". Anti-intuitive, but John normally has a reason...

+13  A: 

This wasn't available when this question was answered, but you can also use the live() method to enable/disable events.

$('#myimage:not(.disabled)').live('click', myclickevent);

$('#mydisablebutton').click( function () { $('#myimage').addClass('disabled'); });

What will happen with this code is that when you click #mydisablebutton, it will add the class disabled to the #myimage element. This will make it so that the selector no longer matches the element and the event will not be fired until the 'disabled' class is removed making the .live() selector valid again.

This has other benefits by adding styling based on that class as well.

MacAnthony
Nice way to use `live()`, never thought about using it this way before ... Aside from coding convenience, is it faster or does it offer any other advantages to using bind/unbind?
chakrit
If you are adding/removing elements, then there are definite performance advantages over bind as the binding with live is only done once. jQuery seems to be moving to more live and delegate events rather than binding to specific elements.
MacAnthony
+1  A: 

Thanks for the information. very helpful i used it for locking page interaction while in edit mode by another user. I used it in conjunction with ajaxComplete. Not necesarily the same behavior but somewhat similar.

function userPageLock(){ $("body").bind("ajaxComplete.lockpage", function(){ $("body").unbind("ajaxComplete.lockpage"); executePageLock();
}); };

function executePageLock(){ //do something }

jquery_user