jQuery event.data

Beginner
5 min read
Updated: Nov 2026
3 examples
Events

Definition

event.data is an optional object you pass when binding with .on(). jQuery attaches it to the event object so the handler can read custom fields. Added in jQuery 1.1.

Syntax

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

jQuery
event.data   // Object from .on(..., data, handler) — or undefined

// Direct bind
$("#btn").on("click", { id: 42 }, function (event) {
  event.data.id;   // 42
});

// Delegation
$("#list").on("click", "li", { group: "A" }, handler);

// Official loop pattern — capture i per button
$("button").eq(i).on("click", { value: i }, handler);
CallWhat it does
eventsEvent type string (e.g. "click")
selectorDelegation filter
dataObject → becomes event.data
handlerfunction(event) that reads event.data

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

Parameters

ParameterDescription
Step 1Build a plain object with the values you need ({ value: i }, { id: "save" }).
Step 2Pass it to .on() before the handler: .on("click", data, handler).
Step 3For delegation: .on("click", "button", data, handler).

Return Value

Return ValueDescription
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 — Official Loop: Capture i

Pass { value: i } so each button keeps its own index.

jQuery
var logDiv = $("#log");
for (var i = 0; i < 5; i++) {
  $("button").eq(i).on("click", { value: i }, function (event) {
    var msgs = [
      "button = " + $(this).index(),
      "event.data.value = " + event.data.value,
      "i = " + i
    ];
    logDiv.append(msgs.join(", ") + "\\n");
  });
}
Try It Yourself

How It Works

  1. Loop. Five buttons get handlers while i is 0–4.
  2. Capture. { value: i } stores that iteration’s number.
  3. Click. event.data.value is the stored index.

Example 2 — Pass a Simple Object

Attach metadata once; read it on every click.

jQuery
$("#save").on("click", { action: "save", id: 42 }, function (event) {
  $("#out").text(
    event.data.action + " #" + event.data.id
  );
});
Try It Yourself

How It Works

  1. Bind. The object sits between the event name and the handler.
  2. Click. jQuery puts that object on event.data.
  3. Read. Use event.data.action and event.data.id.

Example 3 — Delegation with Data

One parent handler; shared data for every matching child.

jQuery
$("#list").on("click", "li", { list: "todos" }, function (event) {
  $("#out").text(
    event.data.list + ": " + $(this).text()
  );
});
Try It Yourself

How It Works

  1. Attach. The listener lives on #list.
  2. Filter. Only li clicks run the handler.
  3. Data. event.data.list is the shared bind payload.

Frequently Asked Questions

event.data is the optional object you pass when binding with .on() — for example .on("click", { value: 1 }, handler). jQuery attaches it to the event object so the handler can read event.data.value. Available since jQuery 1.1.
Use .on("click", { key: value }, function (event) { … }). Inside the handler, read event.data.key. For delegation: .on("click", "button", { key: value }, handler).
With var i in a loop, every handler often shares the same i — clicks show the final index. Passing { value: i } to .on() stores each iteration’s value on that handler’s event.data.
No. event.data is the object passed at bind time to .on(). .data() stores values on DOM elements. Handler context vs element storage — different APIs.
event.data is often undefined. Guard before reading fields: if (event.data) { … } or always pass an object when you rely on custom keys.
Bind-time eventData from .on() is what populates event.data on both user clicks and .trigger("click"). Extra arguments to .trigger("click", [x]) are additional handler parameters — not a replacement for bind-time data.

Next: event.delegateTarget Property

Read the element where the jQuery handler was attached.

event.delegateTarget Property 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.

10 people found this page helpful