The add() method registers functions on a jQuery Callbacks list so they can run together later via fire(). This tutorial covers syntax, five worked examples, how add() differs from immediate invocation, and practical patterns for plugins and event hooks.
01
Syntax
callbacks.add(fn)
02
Queue
Register first
03
fire()
Run later
04
Multiple
Many at once
05
Plugins
Extensible hooks
06
Order
First-in, first-out
Fundamentals
Introduction
Callbacks are functions you want to run when something happens — a button click, an AJAX response, or a plugin lifecycle event. jQuery’s Callbacks object is a managed list for those functions. The add() method is how you put handlers into that list.
Unlike calling a function directly, add() defers execution until you explicitly trigger the list with fire() or fireWith(). That separation keeps registration and invocation clean — especially when multiple parts of your app subscribe to the same event.
Concept
Understanding the add() Method
callbacks.add(callback) appends one or more functions to an internal queue. Each function you add stays in the list until you remove it with remove(), clear the list with empty(), or replace the whole Callbacks object.
When fire() runs, jQuery walks the list in registration order and calls every function currently stored. With the default Callbacks list (no special flags), each fire() executes all registered handlers again — not only newly added ones.
💡
Beginner Tip
Think of add() as subscribing to a newsletter and fire() as sending the edition. You subscribe first; delivery happens when you choose to fire.
Foundation
📝 Syntax
General form of callbacks.add:
jQuery
callbacks.add( callback [, callback ] )
Parameters
callback — a function to append to the list. Pass multiple functions as separate arguments; each is added in order.
Return value
Returns the same Callbacks object, so you can chain .add() calls or follow with .fire().
Minimal workflow
jQuery
const callbacks = $.Callbacks();
callbacks.add(function () {
console.log("Handler runs on fire()");
});
callbacks.fire(); // invokes the handler
Cheat Sheet
⚡ Quick Reference
Goal
Code
Create a list
const cbs = $.Callbacks()
Add one handler
cbs.add(fn)
Add several handlers
cbs.add(fn1, fn2, fn3)
Run all handlers
cbs.fire(args...)
Remove a handler
cbs.remove(fn)
Check membership
cbs.has(fn)
Compare
📋 add() vs fire()
Registration and execution are separate steps on a Callbacks list.
add()
queue fn
Stores handlers; does not run them
fire()
run all
Invokes every handler in the list
Order
FIFO
First added runs first
remove()
unsubscribe
Drop handlers before the next fire
Hands-On
Examples Gallery
Each example uses $.Callbacks(). Open DevTools or use the Try-it links to run them interactively.
📚 Getting Started
Register a handler, then trigger the list.
Example 1 — Add One Callback and Fire
The smallest useful pattern: create a list, add a function, fire once.
This corrects a common misconception: the second fire() does not run only the new handler. Need fire-once behavior? Create the list with $.Callbacks("once") — covered in the jQuery.Callbacks() tutorial.
Example 4 — Pass Arguments Through fire()
Handlers added with add() receive whatever arguments you pass to fire().
Plugin authors register core logic first, let users add() hooks, then fire() at the right lifecycle moment — a lightweight pub/sub pattern built into jQuery.
Applications
🚀 Use Cases
Event handling — collect handlers for custom events, then fire when the event occurs.
Plugin development — let third-party code extend your widget via add() on exposed Callbacks lists.
Pub/Sub patterns — decouple publishers (fire) from subscribers (add) inside a module.
Deferred internals — jQuery’s Deferred uses Callbacks lists for done, fail, and progress queues.
Animation coordination — queue callbacks to run when a sequence step completes.
🧠 How add() Fits the Callbacks Lifecycle
1
Create list
$.Callbacks() builds an empty, chainable callback manager.
Factory
2
add()
Functions are appended to the internal queue in registration order.
Register
3
fire()
jQuery invokes each handler, passing through any arguments from fire() or fireWith().
Execute
=
🔗
Organized async hooks
Many subscribers, one trigger point — without tangled nested callbacks.
Important
📝 Notes
add() never runs callbacks by itself — always pair with fire() or fireWith().
Default lists re-run every handler on each fire(); use flags on $.Callbacks() for once/memory behavior.
Duplicate function references run twice if added twice.
While the list is locked, new add() calls are ignored until you understand lock semantics (see lock() tutorial).
For most app code today, native events or Promises may suffice — Callbacks shines in jQuery plugins and library internals.
Compatibility
Browser Support
callbacks.add() is part of jQuery’s Callbacks implementation, available in jQuery 1.7+. It works wherever jQuery runs — all modern browsers and IE when paired with a supported jQuery build.
✓ jQuery 1.7+
jQuery Callbacks.add()
Supported in jQuery 1.x, 2.x, and 3.x. Behavior is consistent across browsers because jQuery owns the Callbacks list implementation.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
callbacks.add()Universal
Bottom line: Safe in any project that already includes jQuery. Learn fire() and list flags next for full control.
Wrap Up
🎉 Conclusion
The callbacks.add() method is the entry point for building managed callback lists in jQuery. Register handlers with add(), trigger them with fire(), and compose extensible plugin APIs without spaghetti callbacks.
Practice the five examples above, then explore remove(), fireWith(), and jQuery.Callbacks() flags for advanced control.
Register all hooks before the first fire() when possible
Document exposed Callbacks lists on plugins
Use has() before re-adding the same reference
Pick Callbacks flags (once, memory) upfront
❌ Don’t
Expect add() to invoke handlers immediately
Assume only new handlers run on the second fire()
Add heavy logic without error handling inside hooks
Expose raw lists if callers should not fire() them
Forget to remove() hooks that are no longer needed
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about add()
Build callback lists the jQuery way.
5
Core concepts
+01
add()
Queue handlers
API
▶02
fire()
Run the list
Trigger
1,2,303
Order
FIFO execution
Queue
🛠04
Plugins
Public hooks
Pattern
↻05
Re-fire
All handlers run
Default
❓ Frequently Asked Questions
add() registers one or more functions on a jQuery Callbacks list. Those functions run later when you invoke fire() or fireWith() on the same list. It does not run the callbacks immediately.
Yes. callbacks.add(fn1, fn2, fn3) adds every argument that is a function. Each is appended to the list in the order you pass them.
No. add() only queues functions. You must call fire() or fireWith() to execute them. This separation lets you register handlers first and trigger them when an event or async step completes.
With the default Callbacks list, the next fire() runs every function currently in the list — including ones added earlier. If you need different behavior (run once, remember last args), create the list with flags like "once" or "memory" via jQuery.Callbacks().
jQuery Deferred uses Callbacks lists internally for done, fail, and progress queues. The public add() method belongs to the Callbacks object itself — useful for custom event buses, plugin hooks, and pub/sub patterns outside Deferred.
Yes. Callbacks does not deduplicate by default. The same function reference added twice will run twice on the next fire(). Use has() to check membership or remove() to drop duplicates if needed.
Did you know?
When you call $.ajax().done(fn), jQuery internally uses a Callbacks list and something equivalent to add(fn) on the done queue — the same machinery you can use directly for custom event buses.