jQuery Callbacks add() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is add()?

callbacks.add() appends one or more functions to a jQuery Callbacks list (since jQuery 1.7). It queues handlers; they run when you call fire() or fireWith(). Returns the same Callbacks object for chaining.

Remember
callbacks.add( fn )
callbacks.add( fn1, fn2 )       // multiple args
callbacks.add( [fn1, fn2] )     // array of functions

add()  → queue (does not run)
fire() → run every handler in order

returns the Callbacks object (chainable)
NeedUse
Register a handlercbs.add(fn)
Run the listcbs.fire(args…)
Unsubscribecbs.remove(fn)
Create the list$.Callbacks([flags])

How to Use add()

Create a list, register handlers, then fire when the event or step is ready.

  1. Create a list with $.Callbacks() (optional flags: once, memory, unique, stopOnFalse).
  2. Call add(fn) — or pass several functions / an array.
  3. Trigger with fire() or fireWith(context, args) when you want handlers to run.
  4. Use remove() / empty() when a handler should no longer run.
PieceRequired?Role
Callbacks objectYesFrom $.Callbacks()
callback / arrayYesFunction(s) to append
Return—Same Callbacks object (chainable)

Pseudocode

Pseudocode
list = empty callback queue

add(fn or [fn…]):
  if list is locked or disabled:
    ignore
  else if flag "unique" and fn already in list:
    skip duplicate
  else:
    append fn to list
    if flag "memory" and list already fired:
      run fn now with last args

fire(args…):
  for each fn in list (registration order):
    call fn(args…)

Cheat Sheet

Keep these facts handy while wiring Callbacks lists.

TopicDetail
Syntaxcallbacks.add(callbacks)
ArgumentFunction, multiple functions, or array of functions
Runs now?No (unless memory + already fired)
OrderFirst added → first called (FIFO)
DuplicatesAllowed by default; use "unique" to block
ReturnSame Callbacks object
SincejQuery 1.7

Live Preview

Pick how to register handlers on a fresh $.Callbacks() list, then fire with "hello".

Browser demo of callbacks.add — same idea as the examples below.

Result
callbacks.add(foo)
callbacks.fire("hello")
→ foo: hello

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Add One Callback and Fire

Create a list, register one handler, then fire.

jQuery
var callbacks = $.Callbacks();

callbacks.add(function () {
  console.log("First callback executed");
});

callbacks.fire();
Try It Yourself

How It Works

  1. Create the list. $.Callbacks() starts with an empty queue.
  2. Register. add() stores the function — nothing prints yet.
  3. Fire. fire() walks the list and calls the handler.
  4. Separation. Registration and execution stay independent steps.

Example 2 — Multiple Handlers (Args or Array)

Pass several functions at once, or wrap them in an array — both append in order.

jQuery
var callbacks = $.Callbacks();

function step1() { console.log("Step 1: validate"); }
function step2() { console.log("Step 2: save"); }
function step3() { console.log("Step 3: notify"); }

callbacks.add(step1, step2);   // multiple args
callbacks.add([step3]);        // array form (official API)

callbacks.fire();
Try It Yourself

How It Works

  1. Define named functions. Easier to remove later with remove(step2).
  2. Multi-arg add. add(step1, step2) appends both in order.
  3. Array add. add([step3]) is the official Array overload.
  4. One fire. All three run FIFO — a simple staged pipeline.

Example 3 — Add After an Earlier fire()

On a default list, every fire() runs all handlers currently registered — including earlier ones.

jQuery
var callbacks = $.Callbacks();

callbacks.add(function () {
  console.log("First callback");
});

callbacks.fire();
// → First callback

callbacks.add(function () {
  console.log("Second callback");
});

callbacks.fire();
// → First callback
// → Second callback
Try It Yourself

How It Works

  1. First fire. Only the first handler exists → one log line.
  2. Late add. Second handler joins the same list.
  3. Second fire. Both handlers run — the first is not cleared.
  4. Need once? Use $.Callbacks("once") or remove handlers you no longer want.

Example 4 — Pass Arguments Through fire()

Every handler receives the same arguments you pass to fire().

jQuery
var callbacks = $.Callbacks();

callbacks.add(function (name, score) {
  console.log(name + " scored " + score);
});

callbacks.add(function (name, score) {
  console.log("Log entry:", name, score);
});

callbacks.fire("Ada", 98);
Try It Yourself

How It Works

  1. Register both readers. Each expects (name, score).
  2. Fire with values. fire("Ada", 98) forwards both args.
  3. Same payload. Every handler sees identical arguments.
  4. Custom this? Use fireWith(context, [args]).

Example 5 — Expose a Plugin Hook List

Let consumers add() extensions before your core code fire()s a lifecycle event.

jQuery
var Gallery = (function () {
  var onRender = $.Callbacks();

  function render(items) {
    console.log("Core render:", items.join(", "));
    onRender.fire(items);
  }

  return {
    onRender: onRender,
    render: render
  };
})();

Gallery.onRender.add(function (items) {
  console.log("Analytics: rendered", items.length, "items");
});

Gallery.render(["photo1.jpg", "photo2.jpg"]);
Try It Yourself

How It Works

  1. Hide a Callbacks list. onRender is the public hook surface.
  2. Consumer subscribes. onRender.add(...) registers analytics.
  3. Core fires at the right time. After rendering, fire(items) notifies hooks.
  4. Loose coupling. Core never hard-codes the analytics function.

Edge Cases & Pitfalls

Check these before relying on add() in real projects.

queue

add() does not run handlers

It only queues. Pair with fire() / fireWith() (unless the list has memory and already fired).

re-fire

Default lists re-run everyone

A second fire() calls all current handlers again — not only newly added ones.

memory

memory runs late adds immediately

With $.Callbacks("memory"), adding after a fire invokes the new handler right away with the last args (Deferred-style).

unique

Duplicates allowed by default

The same function reference added twice runs twice. Use "unique" or check has(fn) first.

lock

Locked / disabled lists drop adds

After lock() or disable(), new add() calls do not stick. Inspect locked() / disabled() when debugging.

Browser Support

callbacks.add() is part of jQuery’s Callbacks API (since 1.7). It works in any browser where your jQuery build runs — the list logic lives in jQuery, not as a separate browser API.

✓ jQuery 1.7+ · Browsers

jQuery callbacks.add()

Supported in jQuery 1.x, 2.x, and 3.x across Chrome, Firefox, Safari, Edge, and other browsers with jQuery loaded.

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
callbacks.add() Browsers

Bottom line: Safe wherever jQuery is loaded. Create lists with the right flags (once, memory, unique) before calling add().

Performance Notes

OperationCostBest for
add(fn)O(1) append*Registering handlers
fire()O(n) handlersInvoking the full list
add with uniqueO(n) membership checkPreventing duplicate refs

*Default append is cheap; unique walks the list to reject duplicates. Keep handlers light — heavy work inside callbacks blocks the whole fire() pass. Prefer named function references so remove() can find them later.

Key Takeaways

  • Role: add() queues handlers; fire() runs them.
  • Input: one function, many args, or an array of functions.
  • Flags: memory, unique, and once change add/fire behavior — set them at creation.
  • Plugins: expose a Callbacks list so callers can add() hooks safely.

One line: add() is how you subscribe — fire() is how you publish.

Frequently Asked Questions

add() registers one or more functions on a jQuery Callbacks list. Those functions run later when you call fire() or fireWith() on the same list. add() itself does not run the callbacks.
Yes. Pass separate function arguments — callbacks.add(fn1, fn2) — or an array — callbacks.add([fn1, fn2]). Both append handlers in order. The official API accepts a Function or an Array of functions.
Not on a default list. Handlers wait until fire() or fireWith(). With the memory flag, a handler added after the list has already fired runs right away with the last memorized arguments.
By default, duplicates are allowed and each copy runs on fire(). Create the list with $.Callbacks("unique") to reject duplicate references.
After lock(), new add() calls are ignored. After disable(), the list is shut down and further adds and fires are effectively no-ops. Check locked() / disabled() when debugging missing handlers.
jQuery Deferred uses Callbacks lists internally for done, fail, and progress queues (typically with once and memory). Calling .done(fn) is conceptually like add(fn) on that internal list.

Next: remove()

Unregister specific handlers from a Callbacks list.

remove() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful