The .focus() method is jQuery’s legacy shorthand for binding and triggering the focus event. This tutorial covers .focus(handler) since 1.0, .focus(eventData, handler) since 1.4.3, no-argument .focus() as a trigger, migration to .on("focus") and .trigger("focus"), and why the binding form is deprecated since jQuery 3.3. For the modern focus event API, see the jQuery focus event tutorial.
01
.focus(fn)
Bind handler
02
eventData
Since 1.4.3
03
.focus()
Trigger event
04
.on()
Modern bind
05
.trigger()
Modern fire
06
Deprecated
Since 3.3
Fundamentals
Introduction
Before jQuery 1.7 unified events with .on() and .off(), every common event had a matching shorthand — .focus(), .blur(), .change(), and more. The .focus() method let you react when a user enters a field in one short call: $("#login").focus(function(){ ... }). It remains in jQuery 3.x for backward compatibility but is marked deprecated.
Understanding .focus() matters when reading older login-form and helper-text tutorials in legacy codebases. The method does two distinct jobs: with a function argument it binds; with no arguments it triggers. Modern jQuery splits those roles explicitly — .on("focus", handler) for binding and .trigger("focus") for firing.
Concept
Understanding the .focus() Method
The official jQuery API describes .focus() as a way to bind an event handler to the focus event, or trigger that event on an element. When you pass a handler, jQuery registers it on each matched element and calls it when the user clicks in, tabs to the field, or focus moves programmatically — the same focus event that .on("focus") handles today.
When you call .focus() with no arguments, jQuery synthetically fires the focus event on each matched element. Bound handlers run and keyboard focus moves to the field — useful for auto-focusing the login input on page load. This trigger behavior is equivalent to .trigger("focus").
⚠️
Deprecated since jQuery 3.3
Do not use .focus(handler) or .focus(eventData, handler) in new code. Replace them with .on("focus", handler) or .on("focus", eventData, handler). Replace no-argument .focus() with .trigger("focus"). For delegation via focusin, non-bubbling behavior, and the full modern focus event guide, read the jQuery focus event tutorial.
Foundation
📝 Syntax
The .focus() method has three signatures from the official jQuery API. The binding forms are deprecated; the trigger form remains valid but .trigger("focus") is preferred:
$("#form").on("focus", "input", fn) — not available via .focus()
Parent-level bubbling handler
$("#form").on("focusin", "input", fn) — use focusin, not .focus()
Compare
📋 .focus() vs .on("focus") vs .trigger("focus") vs .off("focus")
Four related APIs — know which one binds, which one fires, and which one removes handlers.
.focus()
deprecated
Legacy bind (.focus(fn)) or trigger (.focus()) — use modern APIs for new code
.on("focus")
bind
Modern binding since 1.7 — supports eventData and delegation on dynamic forms
.trigger("focus")
fire
Programmatically run handlers — clearer than no-arg .focus()
.off("focus")
remove
Unbind handlers — pair with .on("focus") when cleaning up or in SPAs
Hands-On
Examples Gallery
Five examples cover binding, eventData, triggering, migration from .focus() to .on("focus"), and method chaining. Use the Try-it links to run each snippet in the browser. Remember: binding with .focus(handler) is deprecated — these demos show legacy syntax you will encounter in older code.
📚 Binding & Triggering
Legacy .focus() signatures for binding handlers and firing events programmatically.
Example 1 — Bind Handler with .focus(handler)
The canonical legacy pattern — show helper text or highlight when the user enters an input field.
jQuery
$( "#target" ).focus( function() {
$( "#out" ).text( "Handler ran via .focus(function(){ ... })." );
} );
User clicks or tabs into #target
→ handler runs, #out text updates
Deprecated — prefer .on("focus", fn) for new code
How It Works
.focus(fn) registers the function on the input. jQuery internally routes this to .on("focus", fn). The handler runs each time the field gains focus — click, tab key, or programmatic trigger.
Example 2 — Pass eventData with .focus(eventData, handler)
Since jQuery 1.4.3, pass a helper hint object before the handler — it arrives as event.data.
jQuery
$( ".field" ).focus( { hint: "Enter your email" }, function( event ) {
var name = $( this ).attr( "name" );
var empty = $( this ).val().trim().length === 0;
$( "#out" ).text(
name + " | rule: " + event.data.hint + " | empty: " + empty
);
} );
Focus into empty email field → "email | hint: Enter your email | empty: true"
Focus into filled name field → "name | hint: Enter your email | empty: false"
Same handler, shared eventData on every .field input
How It Works
jQuery stores the { hint: "Enter your email" } object and injects it into event.data when any matched .field gains focus. Modern equivalent: .on("focus", { hint: "Enter your email" }, fn).
Example 3 — Trigger Blur with No-Argument .focus()
Calling .focus() without a handler fires the focus event programmatically — auto-focus the login field on page load.
Click or tab into from #target → count increments
Click #fire → $("#target").focus() → same handler runs
No-arg .focus() is trigger shorthand — prefer .trigger("focus")
How It Works
The first .focus(function(){ ... }) binds a handler on #target. The button calls $("#target").focus() with no arguments — that synthetically fires focus. Replace with $("#target").trigger("focus") for clarity.
📈 Migration & Chaining
Compare legacy and modern APIs, and chain after .focus(handler).
Example 4 — Migration: .focus(fn) vs .on("focus", fn)
Both bind the same focus event — .on() is the recommended API for new code.
jQuery
$( "#legacy" ).focus( function() {
$( "#out" ).text( "Legacy: .focus(function(){ ... }) — deprecated since jQuery 3.3." );
} );
$( "#modern" ).on( "focus", function() {
$( "#out" ).text( "Modern: .on('focus', function(){ ... }) — use this for new code." );
} );
Focus into #legacy → legacy deprecation message
Focus into #modern → modern .on("focus") message
Behavior is identical — only the API name differs
How It Works
Under the hood, .focus(fn) calls .on("focus", fn). Migration is a straight rename with no behavior change for simple bindings. The advantage of .on() is delegation — $("#form").on("focus", "input", fn) — which .focus() cannot do.
Example 5 — Chaining After .focus(handler)
.focus(handler) returns the jQuery object — chain show-hint and highlight handlers on login fields.
jQuery
$( "#login" )
.focus( function() {
$( "#hint" ).text( "Helper: enter your username." ).show();
} )
.focus( function() {
$( this ).toggleClass( "active", true );
} );
User clicks or tabs into #login
First handler shows #hint text
Second handler adds .active class
Both handlers run in bind order on each focus
How It Works
Multiple .focus(fn) calls on the same element stack handlers — all run when focus enters. Because each call returns the jQuery object, you can chain them fluently. The same chaining pattern works with .on("focus", fn).
Applications
🚀 Common Use Cases
Reading legacy code — recognize $("#login").focus(fn) as equivalent to .on("focus", fn) in older form scripts.
Show helper text — legacy tutorials bind .focus() to reveal hints when the user enters a field.
Auto-focus on page load — $("#login").focus() after the DOM is ready — replace with .trigger("focus").
Show hint on entry — chained .focus(fn) calls show helper text and mark fields as active.
Maintaining WordPress themes — many admin and front-end scripts still use .focus() — know how to migrate safely.
🧠 How .focus() Routes Through jQuery
1
You call .focus()
With a function argument, jQuery treats it as a bind request. With no arguments, it treats it as a trigger request.
dispatch
2
Bind path
.focus(handler) delegates internally to .on("focus", handler) — the handler is stored in jQuery’s event system.
.on("focus")
3
Trigger path
No-arg .focus() delegates to .trigger("focus") — bound handlers run when focus enters.
.trigger("focus")
4
✎
jQuery object returned
Both paths return the original collection for chaining — .focus(fn).addClass("watched") works the same as .on("focus", fn).addClass("watched").
Important
📝 Notes
Deprecated since jQuery 3.3 — do not use .focus(handler) or .focus(eventData, handler) in new code. Use .on("focus") instead.
Bind with .focus(handler) since jQuery 1.0; eventData form since 1.4.3.
No-argument .focus() triggers the event since jQuery 1.0 — prefer .trigger("focus").
.focus() does not support event delegation — use .on("focus", selector, fn) or .on("focusin", selector, fn) for dynamic forms.
Native focus does not bubble — parent-level handlers need focusin, not direct .focus(fn) on the form.
Remove handlers with .off("focus") — not by calling .focus(undefined).
For the full modern focus event guide — delegation, focusin, and auto-focus patterns — see the jQuery focus event tutorial.
Compatibility
Browser Support
The underlying focus event is a standard DOM focus event supported in every browser jQuery targets. The .focus() method itself is a jQuery API — it works in jQuery 1.x, 2.x, and 3.x. Binding via .focus(handler) is deprecated since 3.3 but still functional; triggering via no-arg .focus() also remains supported.
✓ jQuery 1.0+
jQuery .focus() method
Supported across all jQuery versions you are likely to maintain. Deprecated binding since 3.3 — use .on('focus') for new projects. Native equivalent for binding: element.addEventListener('focus', fn). Native equivalent for trigger: element.focus() or dispatchEvent. Avoid .focus() on hidden elements in Internet Explorer.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.focus()Legacy API
Bottom line: Safe in legacy jQuery projects. Migrate bindings to .on('focus') and triggers to .trigger('focus') when updating login auto-focus and helper-text code.
Wrap Up
Conclusion
The jQuery .focus() method is the legacy shorthand for binding and triggering focus handlers. Use .focus(handler) or .focus(eventData, handler) to bind — both deprecated since jQuery 3.3. Use no-argument .focus() to trigger — equivalent to .trigger("focus").
For new code, prefer .on("focus", fn) for binding and .trigger("focus") for programmatic firing. When you encounter .focus() in older form scripts, recognize it, understand it, and migrate when practical. For delegation, focusin, and the complete modern focus event workflow, continue with the jQuery focus event tutorial.
Write new form validation with deprecated .focus(handler)
Assume .focus() supports delegation — it does not
Confuse .focus(fn) (bind) with .focus() (trigger)
Use .unbind("focus") — it is also deprecated; use .off("focus")
Copy deprecated patterns from old tutorials without modernizing them
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .focus()
Legacy bind, modern migrate.
6
Core concepts
.focus01
.focus(fn)
Bind
Deprecated
data02
eventData
Since 1.4.3
Handler
()03
.focus()
Trigger
No args
.on04
.on("focus")
Modern
Replace
⚡05
.trigger()
Fire
Programmatic
3.306
Deprecated
Since 3.3
Migrate
❓ Frequently Asked Questions
The .focus() method has two roles. With a handler argument — .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 on each matched element, running bound handlers and moving keyboard focus. Both forms have been part of jQuery since 1.0; the binding form 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) or .on('focus', eventData, handler) instead. The no-argument .focus() trigger form still works but .trigger('focus') is clearer and preferred in modern code. Existing legacy code continues to run in jQuery 3.x; migrate when you touch those files.
.focus(handler) registers a callback — it does not fire the event immediately. .focus() with no arguments triggers the focus event programmatically on every matched element, the same as .trigger('focus'). The handler form is deprecated for binding; the no-arg form is a trigger shorthand. Always check whether parentheses contain a function before reading legacy jQuery code.
Replace .focus(fn) with .on('focus', fn). Replace .focus(data, fn) with .on('focus', data, fn). Replace no-arg .focus() with .trigger('focus'). Replace .unbind('focus') cleanup with .off('focus'). Under the hood, .focus(handler) already delegates to .on('focus') — migration is a straight rename with no behavior change for simple bindings.
Yes. jQuery 3.x keeps .focus() for backward compatibility with jQuery 1.x and 2.x codebases. It still binds and triggers correctly. The API docs mark it deprecated to steer new projects toward .on() and .trigger(). Do not use .focus(handler) in new code — prefer the modern API covered in the jQuery focus event tutorial.
Since jQuery 1.4.3, you can pass an object as the first argument before the handler. jQuery attaches it to event.data inside the callback — for example, .focus({ hint: 'email' }, fn) lets fn read event.data.hint when showing helper text. This avoids closures when the same handler serves multiple fields. The modern equivalent is .on('focus', { hint: 'email' }, fn).
Did you know?
jQuery unified event binding in version 1.7 with .on() and .off(), replacing shorthand methods like .focus(), .focus(), and .bind(). The old .focus(handler) still works but is deprecated since 3.3 — under the hood it delegates to .on("focus"). The separate no-argument .focus() remains valid as shorthand for .trigger("focus"), though .trigger("focus") is clearer in modern code.