jQuery .focus() Method

Beginner
⚠️ Deprecated since 3.3
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Form events

What You’ll Learn

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

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.

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.

📝 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:

1. Bind a handler — .focus( handler ) (since 1.0, deprecated 3.3)

jQuery
.focus( handler )
  • handler — function executed each time focus fires: function( event ) { ... }.
  • Returns the jQuery object for chaining.
  • Modern replacement: .on( "focus", handler ).

2. Bind with eventData — .focus( [eventData], handler ) (since 1.4.3, deprecated 3.3)

jQuery
.focus( [eventData], handler )
  • eventData — optional object available in the handler as event.data.
  • Modern replacement: .on( "focus", eventData, handler ).

3. Trigger the event — .focus() (since 1.0)

jQuery
.focus()
  • No arguments — triggers focus on every matched element.
  • Runs bound focus handlers.
  • Preferred replacement: .trigger( "focus" ).

Official jQuery API migration note

jQuery
// Deprecated binding
$( "#target" ).focus( function() {
  alert( "Handler for `focus` called." );
} );

// Modern binding
$( "#target" ).on( "focus", function() {
  alert( "Handler for `focus` called." );
} );

// Deprecated trigger
$( "#target" ).focus();

// Modern trigger
$( "#target" ).trigger( "focus" );

Return value

  • All .focus() signatures return the original jQuery object for chaining.
  • Handler return values are ignored unless you use return false (equivalent to preventDefault() + stopPropagation()).

⚡ Quick Reference

GoalLegacy (.focus)Modern replacement
Bind focus handler$("#login").focus(fn)$("#login").on("focus", fn)
Pass data to handler$("#login").focus({ hint: "email format" }, fn)$("#login").on("focus", { hint: "email format" }, fn)
Trigger focus programmatically$("#target").focus()$("#target").trigger("focus")
Trigger all matched inputs$("input").focus()$("input").trigger("focus")
Remove focus handlers$("#login").off("focus")$("#login").off("focus")
Delegated focus on form inputs$("#form").on("focus", "input", fn) — not available via .focus()
Parent-level bubbling handler$("#form").on("focusin", "input", fn) — use focusin, not .focus()

📋 .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

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(){ ... })." );
} );
Try It Yourself

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
  );
} );
Try It Yourself

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.

jQuery
var count = 0;

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

$( "#fire" ).click( function() {
  $( "#target" ).focus();
} );
Try It Yourself

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." );
} );
Try It Yourself

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 );
  } );
Try It Yourself

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

🚀 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").
  • Shared eventData$(".field").focus({ hint: "email format" }, fn) passes validation metadata without closures.
  • 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").

📝 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.

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 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 jQuery projects. Migrate bindings to .on('focus') and triggers to .trigger('focus') when updating login auto-focus and helper-text code.

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.

💡 Best Practices

✅ Do

  • Use .on("focus", handler) for all new focus bindings
  • Use .trigger("focus") instead of no-arg .focus()
  • Migrate .focus(fn) to .on("focus", fn) when refactoring legacy files
  • Use .off("focus") to remove handlers cleanly
  • Read the modern focus event tutorial for focusin delegation

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about .focus()

Legacy bind, modern migrate.

6
Core concepts
data 02

eventData

Since 1.4.3

Handler
() 03

.focus()

Trigger

No args
.on 04

.on("focus")

Modern

Replace
05

.trigger()

Fire

Programmatic
3.3 06

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.

Learn the modern focus event API

Prefer .on("focus") and .trigger("focus") — full tutorial with five try-it labs.

focus event 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.

6 people found this page helpful