2

I have a button (say, button A) which forwards its click event onto another button (say, button B.)

$("#buttonA").click(function()
{
  $("buttonB").click();
});

But now I want it to do something else in a callback function after the button B event completes when it's triggered from button A. The problem is, the syntax I would expect to use is identical to binding a click event to button B instead:

$("#buttonA").click(function()
{
  $("#buttonB").click(function()
  {
    alert("I want this to happen only after buttonA is clicked and buttonB's onclick event finishes.")
  });
});

I understand why the above won't work, but what should I be doing here instead?

Note: ButtonB is doing an asynchronous ajax form submission.

6
  • Well B would need to somehow say it was done... Commented Apr 9, 2014 at 14:34
  • 3
    Maybe I'm misunderstanding but it seems like what are you wanting to achieve is working in your second bit of code. jsfiddle.net/4xy6n Commented Apr 9, 2014 at 14:34
  • Is buttonB doing something that would take time, like an AJAX request? Commented Apr 9, 2014 at 14:35
  • Hi. Yes, it's doing an Ajax form post. Commented Apr 9, 2014 at 14:37
  • How about calling a javascript function inside button B click event Commented Apr 9, 2014 at 14:37

4 Answers 4

5

when doing this :

$("#buttonA").click(function()
{
  $("#buttonB").click(function()
  {
    alert("I want this to happen only after buttonA is clicked and buttonB's onclick event finishes.")
  });
});

You are binding an event on the #buttonB, which is not what you want. Just insert your code after the .click() and it will execute after the click event is finished :

$("#buttonA").click(function()
{
  $("#buttonB").click();
  alert("I want this to happen only after buttonA is clicked and buttonB's onclick event finishes.");
});

Button b is async

b is async, you shouldn't use anonymous function with .trigger(). here what you could do :

function doSomething(e){
    e = typeof e === "undefined" ? new Event() : e;
    //your code when b is clicked...
    
    //Then ajax
    return $.ajax('www.example.com');
}

$('#buttonB').on('click', doSomething);

$('#buttonA').on('click', function(){
    doSomething().done(function(){
        alert("I want this to happen only after buttonA is clicked and buttonB's onclick event finishes.")
    })
})
Sign up to request clarification or add additional context in comments.

3 Comments

If buttonB is performing an AJAX request, this wouldn't be sufficient and would fire immediately.
Hi. Sorry, I should have mentioned, buttonB is doing an ajax submit.
@ajrskelton hi, i've updated my answer, give it a try.
0

Use the trigger function to trigger a click on button B

$("#buttonA").click(function()
{
  $("#buttonB").click(function()
  {
    alert("I want this to happen only after buttonA is clicked and buttonB's onclick event finishes.")
  });
  $("#buttonB").trigger('click');
});

Comments

0

Your event handlers aren't asynchronous, so you can just put the "callback" right after the trigger for the other event:

$("#buttonA").click(function()
{
  $("#buttonB").click();
  alert("I want this to happen only after buttonA is clicked and buttonB's onclick event finishes.")
});

http://jsfiddle.net/2Rr7V/

Now, if button B is doing something asynchronous itself, such as doing an AJAX call, then I think the best thing to do is to refactor out the asynchronous part into a separate function that can take a callback and call it directly.

IMHO, if you find yourself needing to trigger an event on another control, you are probably doing it wrong and should instead just refactor the bit of code you want into a separate function that can be called without "faking" an event. There are very few cases (but that's not to say there are no cases) where it makes sense to trigger an event.

So something like:

$("#buttonB").click(function() {
    // do any buttonB specific stuff
    DoAsyncThing();
});

$("#buttonA").click(function() {
    // do any pre-async stuff
    DoAsyncThing(function() {
        alert("Async thing is done");
    });
});

function DoAsyncThing(callback) {
    $.ajax(...).done(function() {
        if (callback && typeof callback === "function") {
            callback();
        }
    });
}

Or you even just return the promise from the ajax request so you can attach a done handler to it.

Comments

0

Thank you for the input, everyone

I eventually solved the problem with the following jquery.Callback code, which appears to be working as expected:

function clickB()
{
    $("buttonB").click();
}

function doAlert()
{
    alert("Happens after B!");
}

function doA()
{
var callbacks = $.Callbacks();
    callbacks.add(clickB);
    callbacks.add(doAlert);
    callbacks.fire();
}

$("buttonA").click(function()
{
    doA();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.