jQuery .on() Method

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

What Is .on()?

.on(events [, selector] [, data], handler) attaches event handlers to the selected elements (since 1.7). Omit selector for a direct bind; pass a selector for delegated events on current and future descendants.

Remember
// Direct
$("p").on("click", function () {
  alert($(this).text());
});

// Delegated (dynamic children OK)
$("body").on("click", "p", function () {
  $(this).after("

Another paragraph!

"); }); // Remove with .off() $("body").off("click", "p");
NeedUse
Attach handlers.on()
Remove handlers.off()
Run once.one()
Fire an event.trigger()
Legacy APIs.delegate() / .live()

How to Use .on()

Pick direct binding for fixed elements, or delegation for lists that grow.

  1. Select the element that will own the listener (button, list, form, or document).
  2. Call .on(events, handler) for a direct bind.
  3. Or call .on(events, selector, handler) to handle matching descendants.
  4. Optional: pass data, use namespaces (click.myPlugin), or an events object map.
  5. Clean up later with a matching .off().
PieceRequired?Role
eventsYesEvent name(s) / namespaces, or a map of event → handler
selectorOptionalFilter descendants (delegation). Omit for direct binds
dataOptionalAvailable as event.data in the handler
handlerYes*Function to run, or false (*not needed with an events map)

Pseudocode

Pseudocode
attach listener on ROOT for EVENTS

if no SELECTOR (direct):
    when event reaches ROOT → run HANDLER
    this = ROOT

if SELECTOR (delegated):
    when event bubbles to ROOT:
        if a matching descendant is on the path:
            run HANDLER
            this = that matched descendant

new matching children:
    work automatically (delegated only)

Cheat Sheet

Core facts while wiring UI events.

TopicDetail
ReturnsjQuery (chainable)
Since1.7
Direct.on(events, handler)
Delegated.on(events, selector, handler)
thisBound element (direct) or matched descendant (delegated)
falseShorthand for function () { return false; }
Remove.off() with matching filters

Live Preview

Switch between direct and delegated binding — then add items and click them.

After a direct bind, new items will not respond until you rebind — delegated items will.

Log
Ready — Run “delegated”, then click an item.

jQuery Examples

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

Example 1 — Direct Click

Bind a click handler straight to matching paragraphs.

jQuery
$("p").on("click", function () {
  $("#out").text($(this).text());
});
Try It Yourself

How It Works

  1. Select. Current p elements get a listener each.
  2. Click. Handler runs; this is that paragraph.
  3. Limit. Paragraphs added later are not covered.
  4. Fix. Use delegation (next example) for dynamic content.

Example 2 — Delegated Dynamic Children

One listener on body — new paragraphs stay clickable.

jQuery
$("body").on("click", "p", function () {
  $(this).after("

Another paragraph!

"); });
Try It Yourself

How It Works

  1. Root. Listener sits on stable body.
  2. Filter. Only bubbled events from matching p run the handler.
  3. This. this is the matched paragraph, not body.
  4. Future. Appended paragraphs match automatically.

Example 3 — Event Data

Pass an object and read it as event.data.

jQuery
function myHandler(event) {
  $("#out").text(event.data.foo);
}

$("button").on("click", { foo: "bar" }, myHandler);
Try It Yourself

How It Works

  1. Pass. The data object is stored with the binding.
  2. Fire. On click, jQuery sets event.data.
  3. Read. Handler uses event.data.foo.
  4. Reuse. Same named function can be bound with different data objects.

Example 4 — Events Object Map

Attach several handlers in one call.

jQuery
$("#box").on({
  click: function () {
    $("#out").text("click");
  },
  mouseenter: function () {
    $("#out").text("enter");
  },
  mouseleave: function () {
    $("#out").text("leave");
  }
});
Try It Yourself

How It Works

  1. Map. Keys are event names; values are handlers.
  2. Bind. jQuery registers each pair on the element.
  3. Interact. Different events update the same output.
  4. Alt. Space-separated names: .on("mouseenter mouseleave", fn).

Example 5 — Custom Event

Listen for a name you invent, then fire it with .trigger().

jQuery
$("p").on("myCustomEvent", function (event, myName) {
  $(this).text(myName + ", hi there!");
});

$("#fire").on("click", function () {
  $("p").trigger("myCustomEvent", ["John"]);
});
Try It Yourself

How It Works

  1. Name. Register myCustomEvent with .on().
  2. Trigger. Button fires that event and passes extra data.
  3. Args. Extra trigger values arrive after event.
  4. Use. Decouple components without a native DOM event type.

Edge Cases & Pitfalls

Watch for these when attaching handlers.

timing

Elements must exist for direct binds

Direct .on() only attaches to elements present now. Prefer delegation or wait for DOM ready.

root

Delegate close to the target

Avoid dumping every delegated handler on document in large pages — attach nearer the content.

svg

Delegated handlers and SVG

Official note: delegated event handlers do not work for SVG — bind directly when needed.

bubble

Some events do not bubble

load, scroll, and error (and a few others) are poor fits for delegation — attach them directly.

Browser Support

.on() has been part of jQuery since 1.7 and is the standard event-binding API. It works wherever your loaded jQuery version runs.

✓ Stable · jQuery 1.7+

.on()

Works in every browser that runs your loaded jQuery version. Prefer a current jQuery 3.x build for modern projects.

100% With jQuery 1.7+
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
.on() Universal

Bottom line: Production-ready. Use .on() / .off() for all new event code; migrate away from .bind(), .delegate(), and .live().

Performance Notes

ApproachCostBest for
One delegated handler near targetsLowLong lists / dynamic UI
Handler per rowHigherAvoid on large tables
Many filters on documentHigherPrefer closer roots + simple selectors
mousemove / scroll floodsHighThrottle / keep handlers light

Delegation is usually cheaper than mass direct binds. Keep selectors simple (tag, #id, .class) when filtering bubbled events.

Key Takeaways

  • Role: one modern API for direct and delegated event binding.
  • Delegate: pass a selector so future children stay covered.
  • Cleanup: pair with .off(); use namespaces in plugins.
  • Replace: prefer .on() over .bind(), .delegate(), and .live().

One line: .on() is how modern jQuery attaches events — direct for fixed elements, delegated for dynamic ones.

Frequently Asked Questions

.on() attaches event handlers to the selected elements. Since jQuery 1.7 it is the unified binding API for direct handlers, delegated handlers, namespaces, event data, and multiple events — replacing .bind(), .delegate(), and .live().
Direct: $("button").on("click", fn) runs for that button. Delegated: $("#list").on("click", "li", fn) listens on #list and runs for matching li descendants — including ones added later.
Use .off() with matching filters: event type, namespace, selector, and/or handler function. Example: $("#list").off("click", "li", fn).
Append a suffix like click.myPlugin. You can then .off(".myPlugin") or .trigger("click.myPlugin") without affecting other click handlers. Namespaces are not hierarchical — one matching name is enough.
Yes. .on("click", { foo: "bar" }, handler) puts that object on event.data. If data is a string and you also need a selector, pass the selector explicitly (or null) so the string is not treated as a selector.
Yes. .on("myCustomEvent", fn) listens for events you fire with .trigger("myCustomEvent") or .trigger("myCustomEvent", [extraArgs]).

Next: .one()

Attach a handler that automatically removes itself after one run.

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