Samantha J T Star
Samantha J T Star

Reputation: 32808

Can I chain function calls in jQuery?

I want to execute the following:

$('#AccountID').change(SelectAccounts);

and then (SelectProducts)

Is there some way I can make the function SelectProducts execute after SelectAccounts all on the same line?

Upvotes: 1

Views: 2096

Answers (3)

xkeshav
xkeshav

Reputation: 54022

use jQuery.Callbacks() added from jQuery v1.7

var callbacks = $.Callbacks();
callbacks.add( SelectAccounts);
callbacks.add( SelectProducts );
//then use callbacks.fire() with supported flags

Upvotes: 0

moey
moey

Reputation: 10887

Those are two different functions so you'd need to call them separately.

$('#AccountID').change(function () {
  SelectAccounts();
  SelectProducts();
});

In case you need to control the exact execution time for the second function, this discussion might be useful: Upon Link Click and setTimeout

Upvotes: 1

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

How about:

$("#AccountID").change(SelectAccounts).change(SelectProducts);

(in jQuery, event handlers are executed in the order they're bound. SelectAccounts will always run before SelectProducts)

Upvotes: 5

Related Questions