jQuery .bind() Method

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

Definition

.bind() (since 1.0, deprecated 3.0) attaches a handler to one or more events on every matched element. It only supports direct binding — no delegation. New code should use .on() (since 1.7), which accepts the same arguments plus a delegation selector.

Syntax

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

jQuery
// Legacy (deprecated since jQuery 3.0)
$("#foo").bind("click", handler);
$("#foo").bind("click", { name: "Jane" }, handler);   // eventData
$("#foo").bind({ click: onClick, mouseenter: onEnter }); // events object
$("#foo").bind("click.myPlugin", handler);            // namespace

// Recommended (jQuery 1.7+)
$("#foo").on("click", handler);
$("#foo").on("click", { name: "Jane" }, handler);
$("#foo").on({ click: onClick, mouseenter: onEnter });
$("#foo").on("click.myPlugin", handler);

// Remove with the matching modern API
$("#foo").off("click.myPlugin");
CallWhat it does
.bind(eventType, handler)Attach handler for one or more space-separated event types
.bind(eventType, eventData, handler)Same, with data available as event.data (since 1.4.3)
.bind(eventType, false)Shorthand for a handler that returns false (since 1.4.3)
.bind(events)Object map of { type: handler } pairs (since 1.4)
.bind("type.namespace", handler)Tag the binding so it can be removed as a group later

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

Parameters

ParameterDescription
eventTypeA string with one or more event types separated by spaces, optionally with namespaces: "click", "keydown.myPlugin".
eventDataOptional object passed to the handler as event.data. Read when the event fires, not when you bind.
handlerfunction(eventObject) that runs each time the event fires. Inside it, this is the DOM element.
eventsAn object where each key is an event type (namespaces allowed) and each value is a handler function.
falsePass false instead of a function to prevent the default action and stop bubbling.

Return Value

Return ValueDescription
jQuery objectThe original collection, so you can chain more methods such as .addClass() or .on().

jQuery Examples

Example 1 — Basic .bind() and the .on() Equivalent

Attach a click handler the legacy way, then see the one-word migration.

jQuery
// Legacy
$("#foo").bind("click", function () {
  $("#log").text("clicked via .bind()");
});

// Modern replacement
$("#foo").on("click", function () {
  $("#log").text("clicked via .on()");
});
Try It Yourself

How It Works

  1. Select. $("#foo") picks the target element.
  2. Bind. .bind("click", fn) registers a direct handler.
  3. Migrate. Rename .bind to .on — arguments stay the same.

Example 2 — eventData and the Events Object

Pass data into a handler, and bind several event types in one call.

jQuery
// eventData → event.data
$("#greet").bind("click", { name: "Jane" }, function (event) {
  $("#log").text("Hello, " + event.data.name);
});

// events object: several types at once
$("#box").bind({
  mouseenter: function () { $(this).addClass("hot"); },
  mouseleave: function () { $(this).removeClass("hot"); },
  click: function () { $("#log").text("box clicked"); }
});
Try It Yourself

How It Works

  1. Data. The object before the handler becomes event.data.
  2. Map. Each key in the events object is an event type with its own handler.
  3. Same API. .on() accepts both forms unchanged.

Example 3 — Namespaces and Removal (.unbind() vs .off())

Tag handlers with a namespace, then remove the whole group at once.

jQuery
$("#foo").bind("click.myEvents", function () {
  $("#log").text("myEvents click");
});
$("#foo").bind("click.other", function () {
  $("#log").append(" · other click");
});

// Legacy removal
$("#foo").unbind(".myEvents");

// Modern removal (works for .bind() and .on() handlers)
// $("#foo").off(".myEvents");
Try It Yourself

How It Works

  1. Namespace. click.myEvents is still a click handler, tagged for later cleanup.
  2. Group remove. .unbind(".myEvents") removes every handler in that namespace.
  3. Prefer .off(). It removes handlers from both .bind() and .on().

Frequently Asked Questions

.bind() attaches an event handler directly to each matched element. You can pass an event type (or several, space-separated), optional eventData, and a handler function, or an object that maps event types to handlers.
Yes. .bind() has been deprecated since jQuery 3.0. It still works in 3.x, but it only supports direct binding. Use .on() (available since 1.7) for all new code.
Replace the method name and keep the arguments: .bind("click", fn) becomes .on("click", fn), .bind("click", data, fn) becomes .on("click", data, fn), and .bind({ click: fn }) becomes .on({ click: fn }). Namespaces such as click.myPlugin work the same way.
.bind() only attaches handlers directly to the selected elements. .on() does the same, and also supports delegated handlers via an optional selector argument, so it works for elements added to the page later.
Use .unbind() for legacy code or .off() for modern code. Both accept an event type, a handler reference, or a namespace such as .off(".myPlugin"). .off() works for handlers added with .bind() and .on().
It is shorthand for a handler that calls event.preventDefault() and event.stopPropagation(). The modern equivalent is .on("click", false). Use .off("click") to remove it.

Next: .unbind() Method

See how to remove handlers attached with .bind() and why .off() replaced it.

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