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
Fundamentals
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.
Concept
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.”
Foundation
📝 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.
First callback: Hello, world!
Second callback: HELLO, WORLD!
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
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.
Applications
🚀 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.
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.
Important
📝 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.
Compatibility
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 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.fireWith()Universal
Bottom line: Safe in jQuery projects. Use fire() for simple calls; use fireWith() when this matters.
Wrap Up
🎉 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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about fireWith()
Context-aware invocation for Callbacks lists.
5
Core concepts
🔗01
fireWith()
Invoke + bind
API
this02
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.