The focus event fires when an element gains focus — the user clicks into a field, tabs to it, or you call .trigger("focus"). This tutorial covers binding with .on("focus"), passing eventData, triggering programmatically, comparing focus with blur and focusin, and practical patterns like helper text and auto-focus on load.
01
.on()
Bind handler
02
eventData
Pass data
03
.trigger()
Fire focus
04
vs blur
Gain / lose
05
focusin
Delegation
06
Since 1.7
Modern API
Fundamentals
Introduction
Every login form, search box, and modal dialog depends on knowing which field currently has keyboard focus. The focus event tells you an element just became the active target — ideal for showing hints, highlighting borders, or moving the cursor into the first empty field.
The official jQuery API distinguishes the focusevent (bound with .on("focus") since 1.7) from the deprecated .focus()method used in older code. To programmatically focus a field, use .trigger("focus") — available since jQuery 1.0.
Concept
Understanding the focus Event
The focus event is sent to an element when it gains focus. By default it applies to form controls — <input>, <textarea>, <select> — and links (<a>). Modern browsers extend it to any element with an explicit tabindex attribute.
An element gains focus when the user clicks it, tabs to it with the keyboard, or when JavaScript calls .trigger("focus"). Focused elements are usually highlighted by the browser (for example, a dotted outline) and receive keyboard events first. jQuery normalizes cross-browser quirks — including IE’s asynchronous native focus — so your handlers behave consistently.
💡
Beginner Tip
Pair focus with blur: show helper text or a blue border on .on("focus"), hide or validate on .on("blur"). Only call .trigger("focus") on visible elements — triggering focus on a hidden field throws an error in Internet Explorer.
Foundation
📝 Syntax
The modern jQuery API for the focus event has two main forms — binding a handler and triggering the event:
.on() and .trigger() return the original jQuery object for chaining.
Handler return values are ignored unless you use return false (equivalent to preventDefault() + stopPropagation()).
Cheat Sheet
⚡ Quick Reference
Goal
Code
Bind focus handler
$("#email").on("focus", fn)
Pass data to handler
$("#email").on("focus", { hint: "Email" }, fn)
Delegated focus on inputs
$("#form").on("focus", "input", fn)
Focus element programmatically
$("#login").trigger("focus")
Run handlers without moving focus
$("#login").triggerHandler("focus")
Auto-focus on page load
$(function(){ $("#login").trigger("focus"); })
Remove focus handlers
$("#email").off("focus")
Bubbling alternative
$("#form").on("focusin", "input", fn)
Compare
📋 focus vs blur vs focusin vs deprecated .focus()
Four related APIs — pick the event that matches when focus enters and whether you need bubbling.
focus
gains focus
Fires on the element that received focus — does not bubble; best for single-field handlers
blur
loses focus
Opposite of focus — fires when the user leaves a field; pair for enter/exit UX
focusin
bubbles
Like focus but bubbles — one handler on a parent form covers all nested inputs
.focus() method
deprecated
Old binding shorthand — use .on("focus") and .trigger("focus") instead
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Examples 3–5 expand on the official focus demos for hints, auto-focus, and delegation. Use the Try-it links to run each snippet in the browser.
📚 Binding & Triggering
Official jQuery demos for focus handlers and programmatic triggers.
Example 1 — Official Demo: Bind Handler on #target
Alert when the first input field gains focus — click into it or tab to it.
Click into #target or tab to it
→ alert: "Handler for focus called."
Handler runs once each time the field gains focus
How It Works
.on("focus", fn) registers the function on the input. jQuery calls it when the browser fires focus — after a click inside the field or keyboard navigation to it.
Example 2 — Official Demo: #other Triggers Focus on #target
One button programmatically moves focus to an input field.
Click #other → $("#target").trigger("focus")
→ cursor moves to #target, alert runs
Same as clicking or tabbing into the field directly
How It Works
.trigger("focus") synthetically fires focus on #target, moving keyboard focus and running bound handlers. Ensure the element is visible — hidden elements error in IE.
📈 Practical Form Patterns
Official extended demos for hints, auto-focus, and delegation.
Example 3 — Official Demo: Show Hint on Focus with Fade
Display a “focus fire” label next to each input when it gains focus.
Focus first input → "focus fire" span appears, fades over 1s
Focus second input → same animation on that row
$(this).next("span") targets the sibling hint element
How It Works
This is the official jQuery demo pattern. Inside the handler, $(this) is the focused input. The adjacent <span> flashes visible feedback — adapt it to show persistent helper text instead of fading out.
Example 4 — Official Demo: Auto-Focus Login on Page Load
Move focus to the login field as soon as the DOM is ready.
Page loads → #login receives focus automatically
User can type immediately without clicking the field
Equivalent to HTML autofocus attribute via jQuery
How It Works
Wrapping .trigger("focus") in $(function(){ ... }) waits until the DOM is parsed. The login input becomes the active element — a standard pattern for sign-in pages and modals.
Example 5 — One Handler for All Inputs via focusin
Because focus does not bubble, use focusin on the form to highlight every input — including fields added later.
Tab into any input → .active class on that field only
Click "Add field" → new input gets same highlight behavior
One handler on #signup-form covers all inputs
How It Works
focusin bubbles from the input to #signup-form. jQuery’s delegated .on("focus", "input", fn) uses the same mechanism internally. One binding covers current and future inputs.
Applications
🚀 Common Use Cases
Helper text — show format hints when the user enters a field; hide on blur.
Active styling — add a blue border or .active class on focus for clear visual feedback.
Auto-focus login — $("#login").trigger("focus") on page load or when a modal opens.
Keyboard shortcuts — a button that calls $("#search").trigger("focus") to jump to the search box.
Clear on focus — select all text in an input when the user clicks in: $(this).select() inside the focus handler.
Dynamic forms — delegated focusin on the form for inputs added via AJAX.
🧠 How the focus Event Flows
1
User targets field
Click inside the input, tab from the previous field, or script calls .trigger("focus").
enter
2
Previous field blurs
If another element had focus, it fires blur first — only one element is focused at a time.
blur
3
focus event fires
Browser sends focus to the new element — jQuery runs bound handlers.
focus
4
✎
Handler runs
Your function executes — show hints, highlight, or select text. $(this) is the field that gained focus.
Important
📝 Notes
Bind with .on("focus", handler) since jQuery 1.7 — not the deprecated .focus(handler) method.
Trigger with .trigger("focus") since jQuery 1.0.
Native focus does not bubble — use focusin or jQuery delegation for parent-level handlers.
jQuery maps focus to focusin in delegation methods since 1.4.2.
In Internet Explorer, native focus is asynchronous — jQuery 3.7+ uses focusin as the backing event in IE for consistency.
Do not .trigger("focus") on hidden elements in IE — it throws an error. Use visible fields only.
Use .triggerHandler("focus") to run handlers without moving keyboard focus.
Use .off("focus") to remove handlers — avoid deprecated .unbind("focus").
Compatibility
Browser Support
The focus event is a standard DOM focus event supported in every browser jQuery targets. jQuery’s .on("focus") (since 1.7) and .trigger("focus") (since 1.0) normalize cross-browser behavior — including IE’s asynchronous focus quirk handled in jQuery 3.7+.
✓ jQuery 1.7+
jQuery focus event
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery versions. Native equivalent: element.addEventListener('focus', fn) — jQuery adds collection binding, eventData, delegation via focusin, and .trigger() in one chainable API.
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
focusUniversal
Bottom line: Safe in any jQuery project. Prefer .on('focus') over deprecated .focus() for binding. Use focusin for delegation on parent forms. Only .trigger('focus') visible elements in IE.
Wrap Up
Conclusion
The jQuery focus event is the standard way to respond when a user enters a field. Bind handlers with .on("focus", fn), pass optional eventData, and move focus programmatically with .trigger("focus") — especially for login auto-focus on page load.
Remember that focus does not bubble — for one handler on an entire form, use focusin or jQuery’s delegated focus syntax. Pair focus with blur for complete enter/exit UX. Continue with the blur event tutorial for the opposite side of the focus lifecycle.
Bind with .on("focus", handler) — the modern, delegation-capable API
Pair focus and blur for show/hide helper text patterns
Use focusin or delegated focus for dynamic forms with many inputs
Auto-focus the primary field on login pages with .trigger("focus")
Use .triggerHandler("focus") when you only need handlers without moving focus
❌ Don’t
Use deprecated .focus(handler) for new code — use .on("focus")
Expect focus to bubble — use focusin for parent-level handlers
Trigger focus on hidden elements in IE — it causes an error
Steal focus aggressively in handlers — it frustrates keyboard users
Forget accessibility — visible focus indicators help all users navigate forms
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about the focus event
Bind, trigger, enter fields.
6
Core concepts
.on01
.on("focus")
Bind
API
⚡02
.trigger()
Fire
Programmatic
↔03
vs blur
Opposite
Pair
form04
focusin
Delegate
Bubbles
🔑05
Auto-focus
On load
Login
.off06
.off("focus")
Unbind
Cleanup
❓ Frequently Asked Questions
The focus event fires when an element gains focus. The user might click into a field, tab to it from the keyboard, or call .trigger('focus') in code. In modern jQuery, bind handlers with .on('focus', handler) since version 1.7. It applies to form controls, links, and any element with a tabindex attribute.
They are opposites. focus fires when an element receives focus — the user enters the field or tabs to it. blur fires when that element loses focus. Pair them for common UX: .on('focus') to show helper text or highlight a field, .on('blur') to validate or hide hints.
No. Native focus does not bubble up the DOM tree. For event delegation on a parent form, use focusin instead — it bubbles and jQuery maps focus to focusin in delegation methods since 1.4.2. Direct binding with .on('focus', fn) on the input itself is the most common pattern.
Call $('#login').trigger('focus') on a visible element. Available since jQuery 1.0, trigger runs bound focus handlers and moves keyboard focus to the field. Use .triggerHandler('focus') if you only want handlers to run without actually moving focus. Avoid .trigger('focus') on hidden elements in Internet Explorer — it causes an error.
Inside $(function(){ ... }), call $('#login').trigger('focus') after the DOM is ready. This is the jQuery equivalent of the HTML autofocus attribute and matches the official jQuery API example. Ensure the element is visible before triggering focus in IE.
focus fires on the element that gained focus and does not bubble. focusin fires on the element that gained focus and bubbles — so a handler on a parent form can listen for focusin on any nested input. Use focus for single-field handlers; use focusin (or delegated focus via jQuery) for one handler on a dynamic form.
Did you know?
Because native focus does not bubble, jQuery’s event delegation maps focus to the bubbling focusin event under the hood since version 1.4.2. In jQuery 3.7+, IE’s asynchronous native focus is also normalized by using focusin as the backing event — the same approach used for blur and focusout.