jQuery callbacks.fireWith() Method — Context and Args

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

What Is fireWith()?

callbacks.fireWith(context, args) runs every handler on a Callbacks list (since jQuery 1.7) with this set to context and with values from the args array. Returns the same Callbacks object for chaining.

Remember
callbacks.fireWith(window, ["foo", "bar"])
// each handler runs as: fn.call(window, "foo", "bar")

fire("a", "b")              // args only
fireWith(ctx, ["a", "b"])   // this + args array
NeedUse
Args onlycbs.fire(a, b)
Custom this + argscbs.fireWith(ctx, [a, b])
Register handlerscbs.add(fn)
Was it invoked?cbs.fired()

How to Use fireWith()

Pick a context object, pack arguments into an array, then fire.

  1. Create a list and add() handlers that expect a useful this.
  2. Choose the context (element, plugin instance, plain object, …).
  3. Call fireWith(context, [arg1, arg2, …]).
  4. Omit args when handlers need this but no parameters.
PieceRequired?Role
contextNo*Value of this inside each handler
argsNoArray / array-like of arguments (*omit = none)
Return—Same Callbacks object (chainable)

Pseudocode

Pseudocode
function fireWith(callbacks, context, args):
    if callbacks is disabled: return callbacks
    if once and already fired: return callbacks

    argList = args or []
    for each handler in callbacks.list:
        handler.call(context, ...argList)

    mark list as fired
    return callbacks

Cheat Sheet

Keep these facts handy when choosing fireWith over fire.

TopicDetail
Syntaxcallbacks.fireWith([context][, args])
SincejQuery 1.7
args typeArray or array-like
Sets thisYes — to context
Marks firedYes — fired() becomes true
Cousinfire(arguments…)

Live Preview

Fire with different contexts and watch this plus arguments in the log.

Browser demo of callbacks.fireWith() — same idea as the examples below.

Result
Ready — pick an action and click Run.

jQuery Examples

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

Example 1 — Context window and Args Array

Official style: pass window and ["foo","bar"].

jQuery
var log = function (value1, value2) {
  console.log("Received: " + value1 + "," + value2);
};

var callbacks = $.Callbacks();
callbacks.add(log);
callbacks.fireWith(window, ["foo", "bar"]);
// Received: foo,bar
Try It Yourself

How It Works

  1. Add log to the list.
  2. Context. First argument becomes this inside log.
  3. Args array. ["foo","bar"] expands to two parameters.
  4. Result. One fireWith runs every current handler that way.

Example 2 — Custom Object as this

Handlers can read properties from your own context object.

jQuery
var cbs = $.Callbacks();
cbs.add(function (label) {
  console.log(this.id + ": " + label);
});

cbs.fireWith({ id: "box" }, ["ready"]);
// box: ready
Try It Yourself

How It Works

  1. Handler uses this.id — so it needs a real context.
  2. fireWith passes { id: "box" } as this.
  3. Array supplies the label argument.
  4. Use case. Widget/plugin instances and DOM-like objects.

Example 3 — fire() vs fireWith()

Same payload, different control over this.

jQuery
var lines = [];
var fn = function (a, b) {
  lines.push((this && this.tag) + ":" + a + "," + b);
};

var cbs = $.Callbacks();
cbs.add(fn);

cbs.fire("x", "y");
cbs.fireWith({ tag: "ctx" }, ["x", "y"]);

console.log(lines.join("\n"));
// undefined:x,y   (or window-related in sloppy mode)
// ctx:x,y
Try It Yourself

How It Works

  1. fire. Passes "x","y" but does not set a custom this.
  2. fireWith. Sets this.tag to "ctx" and still passes both args.
  3. Array form. ["x","y"] matches the two parameters of fire.
  4. Choose. Need this? Use fireWith. Args only? Use fire.

Example 4 — Context Without Arguments

Omit the args array when handlers only need this.

jQuery
var cbs = $.Callbacks();
cbs.add(function () {
  console.log("id=" + this.id);
});

cbs.fireWith({ id: "solo" });
// id=solo

console.log(cbs.fired());  // true
Try It Yourself

How It Works

  1. One argument. Only the context is provided.
  2. No params. The handler reads this.id only.
  3. Status. fired() still becomes true.
  4. Optional. You may pass [] explicitly for the same effect.

Edge Cases & Pitfalls

Most bugs come from confusing fire’s rest args with fireWith’s array.

array

Second parameter is one array

Write fireWith(ctx, [a, b]), not fireWith(ctx, a, b).

arrow

Arrow functions ignore this

If you need the context, register a normal function, not an arrow — arrows keep lexical this.

disable

Disabled / once behave like fire

After disable(), fireWith is a no-op. With once, only the first call runs handlers.

null

Be careful with null context

Passing null/undefined as context follows normal Function.prototype.call rules — prefer an explicit object when handlers use this.

Browser Support

callbacks.fireWith() has shipped since jQuery 1.7 with $.Callbacks. It works in all modern browsers with jQuery loaded.

✓ jQuery 1.7+

callbacks.fireWith()

Invoke every handler with a chosen this context and an arguments array.

100% Browser support
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() Excellent

Bottom line: Call fireWith(context, [args…]) when handlers need a custom this. Use fire() when you only need to pass arguments.

Performance Notes

ApproachCostBest for
fireWith(ctx, args)O(n) handlersPublish with custom this
fire(args…)O(n) handlersArgs only — slightly simpler call
Heavy work in handlersBlocks the passKeep callbacks light

Cost matches fire() — one call per handler. Prefer lean subscribers; defer expensive work outside the Callbacks pass.

Key Takeaways

  • Role: fire handlers with a chosen this and args array.
  • Args: always pack values into an array (or omit).
  • vs fire: use fire for args-only publishes.
  • Status: also sets fired() to true.

One line: fireWith(context, [args…]) is fire plus control over this — remember the array.

Frequently Asked Questions

fireWith(context, args) runs every handler on the Callbacks list with this set to context and with the values from the args array (or array-like object). It returns the same Callbacks object for chaining.
fire(a, b) passes arguments as separate parameters and does not let you choose this. fireWith(ctx, [a, b]) sets this to ctx and requires arguments in an array. Prefer fireWith when handlers need a DOM node, plugin instance, or other object as this.
Yes — pass an Array or array-like object as the second parameter. Omitting args (or passing undefined) means handlers receive no arguments. Do not write fireWith(ctx, "a", "b") — that is not the API.
Yes. After fireWith() runs, fired() returns true — same as after fire().
Same rules as fire(): disable() makes fireWith() a no-op; with once, only the first successful fireWith() invokes handlers.
When subscribers expect this to be your widget or element — for example fireWith(element, [eventData]) so handlers can use this.id or $(this).

Next: fired()

Check whether a Callbacks list has been invoked at least once.

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

8 people found this page helpful