Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite jquery event handlers

I'll try to explain the problem with a simple code.

var fireClick = function() { alert('Wuala!!!') };  $('#clickme').click(fireclick); $('#clickme').click(fireclick); 

So now it will obviously alert 2 times but i need it alert only once. Tried so many ways, but no result.

Thanks.

like image 228
taras Avatar asked Apr 12 '09 11:04

taras


People also ask

How to override jQuery event?

Use the off() method to override jQuery event handlers. This method is used to remove an event handler. The on() method is used to attach one or more event handler.

How do you override a click event?

You can use $(id). unbind() to clear handlers on that element before adding the new one. Show activity on this post. Maybe you need to "unbind" click event which defined as inline attribute.

How to override click event in JavaScript?

onclick overrides other onclick events. Using document. onclick event to hide a div popup in Javascript, overrides my other button click events.

Which method is overridden for event handling of button click?

OnClickListener in your Activity or Fragment, you have to override onClick method on your class. Firstly, link the button in xml layout to java by calling findViewById() method.


1 Answers

As of jQuery 1.7 you should be using off to remove event handlers and on to add them, though you can still use the click shorthand.

$('#clickme').off('click').on('click', fireclick); $('#clickme').off().on('click', fireclick); 

Original answer:

If you want to replace all click handlers, call unbind first without a function argument. If you want to replace all event handlers, don't specify the event type.

$('#clickme').unbind('click').click(fireclick); $('#clickme').unbind().click(fireclick); 
like image 106
tvanfosson Avatar answered Oct 07 '22 20:10

tvanfosson