jQuery .focus() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Form Events

Definition

.focus() is jQuery’s legacy shorthand for binding or triggering the focus event when an element gains keyboard or mouse focus. Binding is deprecated since 3.3; modern code uses .on("focus") / .trigger("focus").

Syntax

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

jQuery
// Legacy (still works; bind form deprecated since 3.3)
$("#login").focus(function () {
  // field gained focus
});

// Modern replacements
$("#login").on("focus", handler);   // bind
$("#login").trigger("focus");       // fire + move focus

// Auto-focus on load (legacy)
$(function () { $("#login").focus(); });
CallWhat it does
handlerfunction(event) — runs when focus is gained
eventDataExtra data available as event.data
No-arg callMoves focus and runs bound handlers
VisibilityElement must be visible (IE safety)

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

Parameters

ParameterDescription
Step 1Bind with .focus(handler) (legacy) or .on("focus", handler) (preferred).
Step 2Optionally pass eventData: .focus(data, handler) → .on("focus", data, handler).
Step 3Trigger with no-arg .focus() (legacy) or .trigger("focus") (preferred).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Basic .focus(handler)

Update a status line when the user enters a field.

jQuery
$("#target").focus(function () {
  $("#out").text("Handler ran via .focus(function(){ ... }).");
});
Try It Yourself

How It Works

  1. Bind. .focus(fn) registers the callback on #target.
  2. Enter. A click or Tab moves focus into the field.
  3. Fire. jQuery runs the stored handler.

Example 2 — .focus(eventData, handler)

Pass metadata (since 1.4.3). Modern form: .on("focus", data, fn).

jQuery
$(".field").focus({ hint: "Enter your email" }, function (event) {
  var name = $(this).attr("name");
  $("#out").text(name + " | hint: " + event.data.hint);
});
Try It Yourself

How It Works

  1. Data. The object becomes event.data inside the handler.
  2. Focus. Entering any matched field fires the callback.
  3. Read. Use event.data.hint for shared helper text.

Example 3 — No-Arg .focus() Trigger

Fire focus from a button — moves the cursor and runs bound handlers.

jQuery
$("#target").focus(function () {
  $("#out").text("Target focus handler fired.");
});

$("#fire").on("click", function () {
  $("#target").focus(); // prefer .trigger("focus")
});
Try It Yourself

How It Works

  1. Bind. The handler is registered first.
  2. Click. No-arg .focus() triggers the event.
  3. Move. Keyboard focus lands on the input.

Frequently Asked Questions

The .focus() method has two roles. With a handler — .focus(handler) or .focus(eventData, handler) — it binds a function that runs when the matched element gains focus. With no arguments — .focus() — it triggers the focus event, running bound handlers and moving keyboard focus. Binding has been part of jQuery since 1.0 and is deprecated since 3.3.
Yes. The binding signatures .focus(handler) and .focus(eventData, handler) are deprecated since jQuery 3.3. Use .on("focus", handler) instead. The no-argument .focus() trigger still works, but .trigger("focus") is clearer. Legacy code continues to run in jQuery 3.x.
.focus(handler) registers a callback and does not fire immediately. .focus() with no arguments triggers the focus event — the same as .trigger("focus"). Check whether parentheses contain a function before reading legacy code.
Replace .focus(fn) with .on("focus", fn). Replace .focus(data, fn) with .on("focus", data, fn). Replace no-arg .focus() with .trigger("focus"). Cleanup with .off("focus"). Under the hood, .focus(handler) already delegates to .on("focus").
Yes. jQuery 3.x keeps .focus() for backward compatibility. It still binds and triggers correctly. The docs mark binding as deprecated to steer new projects toward .on() and .trigger(). Prefer the modern API in the focus event tutorial.
No in Internet Explorer — triggering focus on a hidden element can throw. Only call no-arg .focus() or .trigger("focus") on visible elements. Use .triggerHandler("focus") if you need handlers without moving focus.

Next: .focusin() Method

Learn the deprecated shorthand for the bubbling focusin event.

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

9 people found this page helpful