Search code examples
jquerychain

how to jump or escape chain in jQuery


How to jumping or escape chain in jQuery.

for example.

$("h3").click(function(){
    //doSomething
    if(~~)  
        // in this case, escape chain(A and B function will be not work)
    else if(~~) 
        // in this case, jump to B case(A function will be not work)
})
.bind(A, function(){
    do A case.
})
.bind(B, function(){
    do B case.
});

is it possible?


Solution

  • The code in the click handler isn't executed until the click actually happens, while the bind calls are handled right after the click handler is applied. It looks like what you want is conditional execution of the various handlers. You could achieve that by setting data on the element in the original click handler, then checking the state in the subsequent handlers, but it would probably be better to create them as standalone functions and simply call them from the single click handler as appropriate.