.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(); });
Call
What it does
handler
function(event) — runs when focus is gained
eventData
Extra data available as event.data
No-arg call
Moves focus and runs bound handlers
Visibility
Element must be visible (IE safety)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Bind with .focus(handler) (legacy) or .on("focus", handler) (preferred).
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.