jQuery .on() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Event Handler

Definition

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

Syntax

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

jQuery
// 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");
CallWhat it does
eventsEvent name(s) / namespaces, or a map of event → handler
selectorFilter descendants (delegation). Omit for direct binds
dataAvailable as event.data in the handler
handlerFunction to run, or false (*not needed with an events map)

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

Parameters

ParameterDescription
Step 1Select the element that will own the listener (button, list, form, or document).
Step 2Call .on(events, handler) for a direct bind.
Step 3Or call .on(events, selector, handler) to handle matching descendants.

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 — 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.

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.

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.

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() Method

Attach a handler that automatically removes itself after one run.

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