Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggling click handlers in Javascript

I have an HTML button to which I attach an event, using jQuery's bind(), like so:

$('#mybutton').bind('click', myFirstHandlerFunction);

In myFirstHandlerFunction, I'd like this handler to replace itself with a new handler, mySecondHandlerFunction, like this:

function myFirstHandlerFunction(e) {
    $(this).unbind('click', myFirstHandlerFunction).bind('click', mySecondHandlerFunction);
}

In the second click handler, mySecondHandlerFunction, I'd like to toggle the button back to its original state: unbind the mySecondHandlerFunction handler and reattach the original handler, myFirstHandlerFunction, like so:

function mySecondHandlerFunction(e) {
    $(this).unbind('click', mySecondHandlerFunction).bind('click', myFirstHandlerFunction);
}

This works great, except for one small detail: because the click event has not yet propagated through each of the button's click handlers, the click event is passed on to the button's next click handler, which happens to be the handler that was just bound in the previous handler. The end result is mySecondHandlerFunction being executed immediately after myFirstHandlerFunction is executed.

This problem can be easily solved by calling e.stopPropagation() in each handler, but this has the negative side-effect of cancelling any other click handlers that may have been attached independently.

Is there a way to safely and and consistently toggle between two click handlers, without having to stop the propagation of the click event?

like image 474
Nathan Friend Avatar asked Jan 03 '13 08:01

Nathan Friend


People also ask

How do I toggle between buttons in JavaScript?

A tgl() method is utilized to toggle a button in JavaScript. In this method, you extract the HTML element by employing the getElementById property, and then the if else-if statement is added to it. If the “value==ON”, toggle the value to “OFF”. If the value is OFF, then the value will be toggled to “ON”.

How do you toggle an element in JavaScript?

Toggling the class means if there is no class name assigned to the element, then a class name can be assigned to it dynamically or if a certain class is already present, then it can be removed dynamically by just using the toggle() or by using contains(), add(), remove() methods of DOMTokenList object within JavaScript ...

What is click handler in JavaScript?

It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.


2 Answers

Update: Since this form of toggle() was removed in jQuery 1.9, the solution below does not work anymore. See this question for alternatives.

It looks like toggle() would solve your problem:

$("#mybutton").toggle(myFirstHandlerFunction, mySecondHandlerFunction);

The code above will register myFirstHandlerFunction and mySecondHandlerFunction to be called on alternate clicks.

like image 65
Frédéric Hamidi Avatar answered Oct 25 '22 17:10

Frédéric Hamidi


Just use a boolean to toggle the functionality of the handler, there's no need to juggle which handler is listening:

$('#mybutton').bind('click', myHandlerFunction);
var first = true;

function myHandlerFunction(e) {
    if(first){
        // Code from the first handler here;
    }else{
        // Code from the second handler here;
    }
    first = !first; // Invert `first`
}
like image 45
Cerbrus Avatar answered Oct 25 '22 17:10

Cerbrus