jQuery event.data

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

What Is event.data?

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.

Remember
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);
NeedUse
Pass data at bind.on("click", data, fn)
Read in handlerevent.data.key
Store on element.data(key, value)
Handler elementevent.currentTarget
Click originevent.target

How to Use event.data

Pass an object when you bind; read the same object inside the handler.

  1. Build a plain object with the values you need ({ value: i }, { id: "save" }).
  2. Pass it to .on() before the handler: .on("click", data, handler).
  3. For delegation: .on("click", "button", data, handler).
  4. Inside the handler, read event.data fields.
  5. Do not confuse with .data() on elements — that is a different API.
PieceRequired?Role
eventsYesEvent type string (e.g. "click")
selectorOptionalDelegation filter
dataOptionalObject → becomes event.data
handlerYesfunction(event) that reads event.data

Pseudocode

Pseudocode
data = { key: value }   // optional object at bind time

element.on("click", data, handler)
// or: parent.on("click", "child", data, handler)

when event fires:
    event.data = data   // same object reference from bind
    call handler(event)
    handler may read event.data.key

// Loop trap (var i):
for i from 0 to 4:
    button[i].on("click", { value: i }, handler)
    // event.data.value is correct per button
    // bare i inside handler is often 5 after the loop

Cheat Sheet

Keep these facts handy while wiring handlers with data.

TopicDetail
TypeObject (or undefined if omitted)
SincejQuery 1.1
Bind form.on(events [, sel] [, data], handler)
Official demo{ value: i } inside a for loop
vs .data()Bind payload vs element storage
Shared objectSame reference for every fire of that bind
Mutates?You can change properties — avoid surprise sharing
Official docsapi.jquery.com/event.data

Live Preview

Click a button from the official loop pattern. Compare event.data.value with bare i — same idea as the official jQuery API demo.

Buttons

Each button bound with { value: i } — event.data.value stays correct; bare i is the final loop value.

Log
Waiting — click a button.

jQuery Examples

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

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.
  4. Trap. Bare i is usually 5 after the loop ends.

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.
  4. Reuse. Same payload every time this handler runs.

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.
  4. Row. $(this) is the matched list item.

Example 4 — One Handler, Many Action Ids

Bind the same function with different event.data per button.

jQuery
function onAction(event) {
  $("#out").text("Action: " + event.data.id);
}

$("#edit").on("click", { id: "edit" }, onAction);
$("#delete").on("click", { id: "delete" }, onAction);
$("#share").on("click", { id: "share" }, onAction);
Try It Yourself

How It Works

  1. Shared fn. One function handles every button.
  2. Different data. Each bind passes its own { id: … }.
  3. Branch. The handler reads event.data.id.
  4. Clean. No need for separate named functions per button.

Example 5 — event.data vs .data()

Bind payload vs values stored on the element.

jQuery
$("#item")
  .data("sku", "A-100")
  .on("click", { source: "toolbar" }, function (event) {
    $("#out").text(
      "event.data.source = " + event.data.source + "\\n" +
      ".data('sku') = " + $(this).data("sku")
    );
  });
Try It Yourself

How It Works

  1. .data(). Stores sku on the element itself.
  2. event.data. Comes from the object passed to .on().
  3. Both useful. Element state vs handler bind context.
  4. Pick. Use event.data for bind-time constants; use .data() for per-element values.

Edge Cases & Pitfalls

Watch for these when using event.data.

loop

var loop closures

Bare i after for (var i …) is usually the final value. Capture with { value: i } or use let.

undefined

No data → often undefined

If you omit eventData, guard before reading: if (event.data) { … }.

share

Same object reference

Mutating event.data changes the shared bind object for later fires. Prefer read-only use.

vs .data()

Not the same as .data()

event.data is bind payload; .data() stores values on elements.

Browser Support

event.data is a jQuery event-object feature (not a native DOM Event field). Available since jQuery 1.1 via .on() / legacy bind APIs. Works wherever your loaded jQuery version runs.

✓ Stable · jQuery API

event.data

Supported across jQuery 1.x–3.x. Prefer .on(events, data, handler) in modern code (jQuery 1.7+).

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
event.data jQuery 1.1+

Bottom line: Safe for production. Pass a plain object at bind time; read event.data inside the handler.

Performance Notes

ApproachCostBest for
.on("click", data, fn)CheapConstants / ids per handler
Delegated + shared dataCheapMany similar children
Huge objects on every bindMore memoryKeep payloads small

event.data is just a reference stored at bind time — almost free to read. Prefer small plain objects. For large per-element state, use .data() on the element instead.

Key Takeaways

  • Role: custom object attached when you bind with .on().
  • Loop fix: { value: i } beats bare var i closures.
  • Read: event.data.key inside the handler.
  • Not .data(): bind payload ≠ element storage.

One line: pass a plain object to .on(), then read it as event.data.

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

Read the element where the jQuery handler was attached.

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

10 people found this page helpful