jQuery callbacks.fireWith() Method — Context and Args

Beginner
5 min read
Updated: Nov 2026
3 examples
Callbacks

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
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
CallWhat it does
contextValue of this inside each handler
argsArray / array-like of arguments (*omit = none)
ReturnSame Callbacks object (chainable)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Create a list and add() handlers that expect a useful this.
Step 2Choose the context (element, plugin instance, plain object, …).
Step 3Call fireWith(context, [arg1, arg2, …]).

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: callbacks.fired() Method — Check If Fired

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

callbacks.fired() Method — Check If Fired tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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