Question

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?

Was it helpful?

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.

OTHER TIPS

If A and B are actually just functions, one of which should be called on click, do:

function A()
{
  ...
}
function B()
{
  ...
}

$("h3").click(function(){
    //doSomething
    if(~~)  
        A();
    else if(~~) 
        B();
})

However, your question is not completely clear.

To end your chain you can use jQuery .end() method; But in this case you have a condition and two different code should run for each conditions. So you can use jQuery $(this) under your condition to refer to what is clicked and run the code based on your condition:

$("h3").click(function(){
    //doSomething
    if(~~)  
        // in this case, escape chain(A and B function will be not work)
        $(this).bind(A, function(){
            //do A case.
         })
    else if(~~) 
        // in this case, jump to B case(A function will be not work)
       $(this).bind(B, function(){
          //do B case.
       });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top