jQuery Callbacks add() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Callback lists

What You’ll Learn

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

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.

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.

📝 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

⚡ Quick Reference

GoalCode
Create a listconst cbs = $.Callbacks()
Add one handlercbs.add(fn)
Add several handlerscbs.add(fn1, fn2, fn3)
Run all handlerscbs.fire(args...)
Remove a handlercbs.remove(fn)
Check membershipcbs.has(fn)

📋 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

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.

jQuery
const callbacks = $.Callbacks();



callbacks.add(function () {

  console.log("First callback executed");

});



callbacks.fire();
Try It Yourself

How It Works

add() stores the function; nothing prints until fire() walks the list and invokes it.

Example 2 — Add Multiple Callbacks at Once

Pass several functions in one add() call — they run in order on the next fire().

jQuery
const callbacks = $.Callbacks();



callbacks.add(

  function () { console.log("Step 1: validate"); },

  function () { console.log("Step 2: save"); },

  function () { console.log("Step 3: notify"); }

);



callbacks.fire();
Try It Yourself

How It Works

jQuery appends each argument in sequence. One fire() executes the whole pipeline — useful for staged workflows.

📈 Practical Patterns

Late registration, arguments, and extensible plugins.

Example 3 — Add After an Earlier fire()

With the default list, every fire() runs all handlers currently in the queue — including ones registered before the previous fire.

jQuery
const callbacks = $.Callbacks();



callbacks.add(function () {

  console.log("First callback");

});



callbacks.fire();

// → First callback



callbacks.add(function () {

  console.log("Second callback");

});



callbacks.fire();

// → First callback  (still in the list)

// → Second callback
Try It Yourself

How It Works

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().

jQuery
const 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

Every handler in the list gets the same arguments. For custom this values, use fireWith(context, args) instead.

Example 5 — Plugin Hook List

Expose a Callbacks list so consumers can add() their own extensions before you fire() lifecycle events.

jQuery
const Gallery = (function () {

  const onRender = $.Callbacks();



  function render(items) {

    console.log("Core render:", items.join(", "));

    onRender.fire(items);

  }



  return {

    onRender: onRender,

    render: render

  };

})();



// Consumer adds a hook

Gallery.onRender.add(function (items) {

  console.log("Analytics: rendered", items.length, "items");

});



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

How It Works

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.

🚀 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.

📝 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.

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 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() Universal

Bottom line: Safe in any project that already includes jQuery. Learn fire() and list flags next for full control.

🎉 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.

💡 Best Practices

✅ Do

  • Name handler functions for readable stack traces
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about add()

Build callback lists the jQuery way.

5
Core concepts
02

fire()

Run the list

Trigger
1,2,3 03

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.

Continue to jQuery.Callbacks()

Learn how to create lists with once, memory, and other flags.

jQuery.Callbacks() 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.

6 people found this page helpful