jQuery .focus() Method

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

What Is .focus()?

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

Remember
// 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(); });
NeedUse
New code (bind).on("focus", fn)
New code (trigger).trigger("focus")
Legacy bind.focus(fn) ⚠ deprecated 3.3
Legacy trigger.focus() (prefer .trigger)
Handlers only.triggerHandler("focus")

How to Use .focus()

Recognize the legacy API in old code, then migrate to .on() / .trigger().

  1. Bind with .focus(handler) (legacy) or .on("focus", handler) (preferred).
  2. Optionally pass eventData: .focus(data, handler) → .on("focus", data, handler).
  3. Trigger with no-arg .focus() (legacy) or .trigger("focus") (preferred).
  4. Only trigger on visible elements — hidden targets can error in IE.
  5. For parent forms / dynamic fields, use focusin — .focus() has no delegation.
PieceRequired?Role
handlerFor bind formfunction(event) — runs when focus is gained
eventDataOptional (1.4.3+)Extra data available as event.data
No-arg callFor triggerMoves focus and runs bound handlers
VisibilityFor triggerElement must be visible (IE safety)

Pseudocode

Pseudocode
// Legacy
call element.focus(handler)
    → bind "focus" on element

call element.focus()
    → trigger "focus" on element
    → move keyboard focus (if visible)

// Modern (jQuery 1.7+)
call element.on("focus", handler)
when user clicks / tabs into element:
    fire "focus" on element
    call handler(event)

to migrate:
    .focus(fn)      → .on("focus", fn)
    .focus(data, fn) → .on("focus", data, fn)
    .focus()        → .trigger("focus")

Cheat Sheet

Quick map from the shorthand to modern APIs.

TopicDetail
Legacy bind$("#el").focus(handler) ⚠ deprecated 3.3
Modern bind$("#el").on("focus", handler)
Legacy trigger$("#el").focus()
Modern trigger$("#el").trigger("focus")
Auto-focus on load$(function(){ $("#login").trigger("focus"); })
Unbind$("#el").off("focus" [, handler])
Since / status1.0 · eventData 1.4.3 · bind deprecated 3.3
Bubbles?No — use focusin for parents
DelegationNot via .focus() — use .on("focus", "input", fn)

Live Preview

Pick an action against a sample input. The demo uses .focus() (still available in jQuery 3.x) so you can see bind vs trigger.

You can also click or tab into the demo input below.

Result
Ready — click the input or pick an action.

jQuery Examples

Short, copy-ready snippets. Prefer .on("focus") for new projects; these demos show the legacy API you will still meet in older code.

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.
  4. Prefer. New code should use .on("focus", fn).

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.
  4. Migrate. Replace with .on("focus", { hint: "…" }, fn).

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.
  4. Prefer. Use .trigger("focus") in new code.

Example 4 — Migrate to .on("focus")

Both bind the same event — .on() is the recommended API for new code.

jQuery
$("#legacy").focus(function () {
  $("#out").text("Legacy: .focus(fn) — deprecated since 3.3.");
});

$("#modern").on("focus", function () {
  $("#out").text("Modern: .on('focus', fn) — use this for new code.");
});
Try It Yourself

How It Works

  1. Legacy. .focus(fn) internally calls .on("focus", fn).
  2. Modern. Bind directly with .on().
  3. Same event. Focus timing and targets are unchanged.
  4. Bonus. Only .on() supports delegation on dynamic forms.

Example 5 — Auto-Focus Login with No-Arg .focus()

Classic legacy pattern: focus the login field as soon as the DOM is ready.

jQuery
$(function () {
  $("#login").focus();
  $("#out").text("#login received focus on page load.");
});
Try It Yourself

How It Works

  1. Ready. $(function(){ … }) waits for the DOM.
  2. Trigger. No-arg .focus() activates #login.
  3. Type. The user can start typing without a click.
  4. Migrate. Prefer .trigger("focus") on a visible field.

Edge Cases & Pitfalls

Watch for these when reading or migrating legacy .focus() code.

deprecated

Bind form deprecated since 3.3

Still works in jQuery 3.x, but new code should use .on("focus").

IE

Hidden elements can error

Do not call no-arg .focus() on a hidden field in IE. Show it first, or use .triggerHandler("focus").

bind vs trigger

.focus(fn) ≠ .focus()

With a function it binds; with no args it triggers. Always check the parentheses.

delegation

No built-in delegation

.focus(fn) cannot target future elements. Use $("#form").on("focus", "input", fn) or focusin.

Browser Support

The underlying focus event is a standard DOM event. The .focus() method is a jQuery API available since 1.0 (eventData since 1.4.3). Binding via .focus(handler) is deprecated since 3.3 but still functional in jQuery 3.x.

✓ Deprecated · jQuery 3.3

jQuery .focus()

Works in jQuery 1.x–3.x. Prefer .on('focus') / .trigger('focus') for new projects. Avoid triggering focus on hidden elements in IE.

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
.focus() Legacy API

Bottom line: Safe in legacy projects. Migrate bindings to .on('focus') and triggers to .trigger('focus') when you touch form scripts.

Performance Notes

ApproachCostBest for
Direct .on("focus") on known fieldsCheapLogin, search, single inputs
Delegated focusin on a formCheapDynamic / many nested fields
Heavy work on every focusExpensiveAvoid — keep handlers light

The shorthand and .on("focus") cost the same for a single bind. Prefer .on() for clarity and delegation. Keep the callback light.

Key Takeaways

  • Role: legacy bind/trigger when an element gains focus.
  • Migrate: .on("focus", fn) and .trigger("focus").
  • Two forms: .focus(fn) binds; .focus() triggers.
  • Visible only: never trigger focus on hidden IE targets.

One line: .focus() is deprecated sugar for .on("focus") / .trigger("focus") — migrate when you touch the file.

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

9 people found this page helpful