jQuery Callbacks fireWith() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Context + args

What You’ll Learn

The fireWith() method runs every handler on a Callbacks list while letting you choose the this value and pass an argument array. This tutorial covers syntax, five examples, how it compares with fire(), and real patterns for plugins and DOM-driven code.

01

Syntax

fireWith(ctx, args)

02

Context

Custom this

03

Args array

[a, b, c]

04

vs fire()

When to pick each

05

Plugins

Instance binding

06

fired()

Sets true

Introduction

You already know that fire() runs all handlers in a Callbacks list. But what if those handlers expect this to be a plugin instance, a configuration object, or a DOM element — not the default global object?

fireWith() solves that. It is the context-aware twin of fire(): same invocation behavior, plus explicit control over this and a clean array-based argument list.

Understanding the fireWith() Method

callbacks.fireWith(context, args) calls each registered handler in order. Inside every handler, this equals context. Values in the optional args array are passed as function parameters — the same way fire(arg1, arg2) forwards variadic arguments.

Like fire(), fireWith() returns the Callbacks object (chainable), respects list flags such as once and stopOnFalse, and sets fired() to true after the first successful invocation.

💡
Beginner Tip

Think of fireWith(ctx, [data]) as “call every handler as if it were a method on ctx, passing data.”

📝 Syntax

General form of callbacks.fireWith:

jQuery
callbacks.fireWith( context [, args ] )

Parameters

  • context — The object used as this inside each handler. Can be any JavaScript value (object, DOM node, plugin instance).
  • args (optional) — An array or array-like object whose elements become handler parameters. Omit or pass undefined to call handlers with no arguments.

Return value

  • The same Callbacks object (supports chaining).

Basic pattern

jQuery
const callbacks = $.Callbacks();

const app = { name: "Dashboard" };



callbacks.add(function (message) {

  console.log(this.name + " says:", message);

});



callbacks.fireWith(app, ["Hello, world!"]);

// → Dashboard says: Hello, world!

⚡ Quick Reference

GoalCode
Fire with custom thiscallbacks.fireWith(ctx, [arg])
Fire without argscallbacks.fireWith(ctx)
Multiple argumentscallbacks.fireWith(ctx, [a, b, c])
Default this is finecallbacks.fire(a, b)
Check if list firedcallbacks.fired()

📋 fire() vs fireWith()

Both invoke handlers; only fireWith() sets this explicitly.

fire()
fire(a, b)

Variadic args; default this

fireWith()
fireWith(ctx,[a,b])

Custom this + args array

Handlers run
yes

Same order, same flags

fired()
true

Both set fired state

Examples Gallery

Each example shows a different reason to reach for fireWith() over plain fire().

📚 Getting Started

Bind handlers to a meaningful this and pass data as an array.

Example 1 — Context and Message Array

Handlers read properties from this while receiving a message argument.

jQuery
const callbacks = $.Callbacks();

const app = { label: "First callback" };



callbacks.add(function (message) {

  console.log(this.label + ":", message);

});

callbacks.add(function (message) {

  console.log("Second callback:", message.toUpperCase());

});



callbacks.fireWith(app, ["Hello, world!"]);
Try It Yourself

How It Works

Every handler sees the same this (app) and the same argument from the array. This mirrors the classic jQuery documentation example, with clearer context naming.

Example 2 — fire() vs fireWith()

Same handler, different this depending on which invoke method you use.

jQuery
const callbacks = $.Callbacks();

const owner = { role: "owner" };



callbacks.add(function () {

  console.log("this.role =", this.role || "(undefined)");

});



callbacks.fire();                    // default this

callbacks.fireWith(owner, []);       // owner as this
Try It Yourself

How It Works

When this.role matters, fireWith(owner, []) is the correct tool. Use fire() when handlers do not depend on a specific object as this.

📈 Practical Patterns

DOM nodes, multiple parameters, and plugin-style APIs.

Example 3 — DOM Element as Context

Pass a button element as this so handlers can read tag names or attributes.

jQuery
const onClick = $.Callbacks();



onClick.add(function (eventType) {

  console.log(

    "Handler on <" + this.tagName + ">:",

    eventType

  );

});



// In a real page, this would be document.querySelector("button")

const fakeButton = { tagName: "BUTTON", id: "save" };

onClick.fireWith(fakeButton, ["click"]);
Try It Yourself

How It Works

Widget and plugin code often fires Callbacks with the target element as context so listeners behave like native DOM event handlers.

Example 4 — Multiple Arguments in One Array

Forward several values to every handler with a single args array.

jQuery
const callbacks = $.Callbacks();

const sensor = { id: "temp-1" };



callbacks.add(function (value, unit, timestamp) {

  console.log(

    this.id + " →",

    value + unit,

    "at",

    timestamp

  );

});



callbacks.fireWith(sensor, [22.5, "°C", "2026-07-17"]);
Try It Yourself

How It Works

fireWith(ctx, [a, b, c]) is equivalent to calling each handler as handler.call(ctx, a, b, c). Wrap all payload values in one array.

Example 5 — Plugin Instance Notification

A mini-plugin fires lifecycle hooks with itself as context.

jQuery
function Slider(options) {

  this.options = options;

  this.onChange = $.Callbacks();

}



Slider.prototype.setValue = function (value) {

  this.value = value;

  this.onChange.fireWith(this, [value]);

};



const slider = new Slider({ min: 0, max: 100 });

slider.onChange.add(function (v) {

  console.log("Slider", this.options.max, "now:", v);

});



slider.setValue(42);
Try It Yourself

How It Works

jQuery plugins and widgets use this pattern internally: subscribers access this.options, this.element, or other instance fields because fireWith bound the plugin as context.

🚀 Use Cases

  • jQuery plugins — notify subscribers with the plugin instance as this.
  • Custom events — fire handlers with the target element or event source as context.
  • Deferred internals — jQuery uses Callbacks (and fireWith) inside its Deferred/promise pipeline.
  • Test doubles — assert handlers received the expected this and argument array.
  • Object-oriented modules — keep handler code reading like instance methods via fireWith(this, args).

🧠 How fireWith() Invokes Handlers

1

add() handlers

Register functions on the Callbacks list.

Register
2

fireWith(ctx, args)

jQuery loops handlers and calls each with .call(ctx, ...args).

Invoke
3

Flags apply

once, memory, and stopOnFalse behave the same as with fire().

Rules
=

Context-aware dispatch

Handlers run with the right this and the payload you supplied.

📝 Notes

  • The second argument must be an array (or array-like), not bare comma-separated values.
  • fireWith() and fire() both set fired() to true.
  • On a once list, only the first fireWith() runs handlers; later calls are ignored.
  • After disable(), fireWith() does nothing — check with disabled() if needed.
  • Returning false from a handler may stop the list when stopOnFalse is enabled.

Browser Support

callbacks.fireWith() is part of jQuery’s Callbacks API since jQuery 1.7 and works wherever jQuery runs.

jQuery 1.7+

jQuery Callbacks.fireWith()

Supported in jQuery 1.x, 2.x, and 3.x. Context binding behaves consistently across browsers.

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

Bottom line: Safe in jQuery projects. Use fire() for simple calls; use fireWith() when this matters.

🎉 Conclusion

The callbacks.fireWith() method invokes every handler on a Callbacks list while binding a custom this and forwarding an optional argument array. It is the go-to choice for plugins, widgets, and any code where handlers behave like methods on a specific object.

Next, learn has() to check whether a particular function is already registered on the list.

💡 Best Practices

✅ Do

  • Wrap args in an array: fireWith(ctx, [data])
  • Use fireWith(this, args) inside class methods
  • Pick a stable context object (plugin instance, element)
  • Document what this and args mean for subscribers
  • Fall back to fire() when this is irrelevant

❌ Don’t

  • Pass raw values as the second arg without wrapping in []
  • Confuse fireWith with fired() (status check)
  • Expect new handlers after once + first fire
  • Fire disabled lists without checking disabled()
  • Mutate the args array while handlers still run

Key Takeaways

Knowledge Unlocked

Five things to remember about fireWith()

Context-aware invocation for Callbacks lists.

5
Core concepts
this 02

context

Sets this

Binding
[] 03

args

Array payload

Params
04

fire()

Simpler sibling

Compare
? 05

fired()

Status after invoke

Status

❓ Frequently Asked Questions

fireWith() invokes every handler on the Callbacks list, just like fire(), but lets you set the this value inside those handlers via the context argument. You can also pass an array of arguments as the second parameter.
Use fireWith(context, args) when handlers rely on a specific this — for example a plugin instance, a DOM element, or a data object. Use fire(args...) when the default this is fine and you only need to pass values.
callbacks.fireWith(context [, args]). context becomes this inside each handler. args is optional and must be an array or array-like list of values forwarded to every callback.
Yes. The first successful fireWith() call marks the list as fired, same as fire(). fired() returns true afterward.
The second parameter should be an array (or array-like object). Wrap values in brackets: fireWith(ctx, [value]) or fireWith(ctx, [a, b, c]). For variadic args without custom context, use fire(a, b, c) instead.
On a disabled list, fireWith() is a no-op — same as fire(). On a locked list, handlers already registered still run; you cannot add new ones until unlocked.
Did you know?

jQuery’s Deferred objects use Callbacks lists internally. When a deferred resolves, jQuery fires done/fail/always queues with fireWith so handlers receive the correct context and argument list — the same mechanism you use in plugin code.

Continue to has()

Learn how to check whether a specific callback function is already on the list.

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