jQuery Focus Event

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

What Is the focus Event?

The focus event fires when an element gains focus — the user clicks into a field, tabs to it, or you call .trigger("focus"). Bind with .on("focus", handler) (jQuery 1.7+). Focus does not bubble; use focusin for parent-level handlers.

Remember
$("#target").on("focus", function () {
  // field gained focus
});

$("#target").trigger("focus");   // move focus + run handlers
$("#target").triggerHandler("focus"); // handlers only (no move)

// focus does NOT bubble → for forms use focusin
$("#form").on("focusin", "input", handler);
NeedUse
Bind on one field.on("focus", fn)
Move focus in code.trigger("focus")
Handlers only.triggerHandler("focus")
One handler for a form.on("focusin", "input", fn)
Lose focus.on("blur", fn)

How to Use focus

Bind a handler, optionally pass data, then trigger when you need to move focus.

  1. Bind with .on("focus", handler) on the element that should react.
  2. Optionally pass eventData: .on("focus", data, handler).
  3. Trigger with .trigger("focus") — only on visible elements.
  4. Use .triggerHandler("focus") when you want handlers without moving focus.
  5. For dynamic forms, prefer delegated focusin on a parent.
PieceRequired?Role
handlerFor bindfunction(event) — runs when focus is gained
eventDataOptionalExtra data available as event.data
VisibilityFor triggerHidden elements can error in IE
Typical targetsYesInputs, textareas, selects, links, tabindex elements

Pseudocode

Pseudocode
call element.on("focus", handler)
    → store handler on element

when user clicks / tabs into element:
    fire "focus" on element
    call handler(event)

call element.trigger("focus")
    if element is hidden (IE):
        error — do not trigger
    else:
        move keyboard focus
        call bound handlers

call element.triggerHandler("focus")
    → run handlers only (no focus move)

for parent form with many inputs:
    use .on("focusin", "input", handler)
    // focus itself does not bubble

Cheat Sheet

Keep these facts handy while wiring form focus UX.

TopicDetail
Bind$("#el").on("focus", handler)
Trigger$("#el").trigger("focus")
Handlers only$("#el").triggerHandler("focus")
Unbind$("#el").off("focus" [, handler])
Bubbles?No — use focusin for parents
vs blurGain focus vs lose focus
vs focusinSame idea, but focusin bubbles
Since.on("focus") 1.7 · .trigger("focus") 1.0
Legacy shorthand.focus() ⚠ deprecated for binding since 3.3

Live Preview

Click into the field, tab to it, or run an action to see .on("focus") and .trigger("focus") in action.

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 based on the official jQuery API demos. Open Try It Yourself for a live editor.

Example 1 — Basic .on("focus")

Official pattern: run a handler when #target gains focus.

jQuery
$("#target").on("focus", function () {
  $("#out").text("Handler for focus called.");
});
Try It Yourself

How It Works

  1. Bind. .on("focus", fn) registers the callback on #target.
  2. Enter. A click or Tab moves focus into the field.
  3. Fire. The browser sends the focus event.
  4. Update. The handler writes a short status message.

Example 2 — Trigger Focus from a Button

Official pattern: #other calls .trigger("focus") on #target.

jQuery
$("#target").on("focus", function () {
  $("#out").text("Target focus handler ran.");
});

$("#other").on("click", function () {
  $("#target").trigger("focus");
});
Try It Yourself

How It Works

  1. Bind. The focus handler is ready on #target.
  2. Click. The button runs .trigger("focus").
  3. Move. Keyboard focus lands on the input.
  4. Safety. Only trigger on visible elements (IE pitfall).

Example 3 — Show Helper Text on Focus

Adapted official demo: show a hint when each input gains focus.

jQuery
$("input").on("focus", function () {
  var name = $(this).attr("name");
  $("#out").text("Focused: " + name + " — enter a value.");
});
Try It Yourself

How It Works

  1. Select. All input elements share one handler.
  2. Focus. Entering a field fires the event.
  3. Identify. $(this) is the focused input.
  4. Hint. Status text explains what to type next.

Example 4 — Auto-Focus Login on Load

Official demo: move focus to #login when the DOM is ready.

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

How It Works

  1. Ready. $(function(){ … }) waits for the DOM.
  2. Trigger. .trigger("focus") activates #login.
  3. Type. The user can start typing without a click.
  4. Visible. Keep the field visible before triggering.

Example 5 — One Form Handler with focusin

Because focus does not bubble, use focusin for a parent form — including fields added later.

jQuery
$("#signup-form").on("focusin", "input", function () {
  $("#out").text("Active field: " + ($(this).attr("name") || "(unnamed)"));
});
Try It Yourself

How It Works

  1. Parent bind. The handler lives on #signup-form.
  2. Bubble. focusin bubbles from the input to the form.
  3. Filter. The "input" selector matches the focused control.
  4. Dynamic. Fields added later are covered automatically.

Edge Cases & Pitfalls

Check these before relying on focus handlers in real forms.

bubble

focus does not bubble

A parent .on("focus", fn) will not see child focus. Use focusin or delegated .on("focus", "input", fn).

IE

Hidden elements error in IE

Do not call .trigger("focus") on a hidden field. Show it first, or use .triggerHandler("focus").

shorthand

.focus(handler) is deprecated

Prefer .on("focus", handler). The shorthand bind form is deprecated since jQuery 3.3.

pair

Pair with blur

Show hints on focus and clean up on blur so helpers do not stick around after the user leaves.

Browser Support

The focus event is a standard DOM event. Bind with .on("focus") since jQuery 1.7; .trigger("focus") has existed since 1.0. As of jQuery 3.7.0, IE uses focusin as the native backing event for focus to avoid async quirks.

✓ jQuery 1.7+

jQuery focus event

Supported in all browsers jQuery targets. Prefer .on('focus') / .trigger('focus'). For parent forms, use focusin. Native equivalent: element.addEventListener('focus', fn).

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 Excellent

Bottom line: Safe for modern and legacy jQuery projects. Avoid triggering focus on hidden elements in IE, and use focusin when you need bubbling.

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

Focus fires often while users tab through a form. Keep callbacks small — toggle a hint, set a class, or update a short label.

Key Takeaways

  • Role: react when an element gains keyboard/mouse focus.
  • API: .on("focus", fn) and .trigger("focus").
  • No bubble: use focusin for parent/form handlers.
  • Visible only: never .trigger("focus") on hidden IE targets.

One line: use .on("focus") for single fields, focusin for forms, and .trigger("focus") only on visible elements.

Frequently Asked Questions

The focus event fires when an element gains focus — the user clicks into a field, tabs to it, or you call .trigger("focus"). Bind handlers with .on("focus", handler) since jQuery 1.7. It applies to form controls, links, and elements with a tabindex.
They are opposites. focus fires when an element receives focus; blur fires when it loses focus. Pair them for UX: .on("focus") to show helper text, .on("blur") to validate or hide hints.
No. Native focus does not bubble. For one handler on a parent form, use focusin (it bubbles). Since 1.4.2, jQuery maps delegated .on("focus", selector, fn) to focusin behind the scenes.
Call $("#login").trigger("focus") on a visible element. It runs bound handlers and moves keyboard focus. Use .triggerHandler("focus") to run handlers without 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 matches the official jQuery API example and is similar to the HTML autofocus attribute. Ensure the element is visible first.
Prefer .on("focus", handler) and .trigger("focus"). The .focus() shorthand binding form is deprecated since jQuery 3.3. See the .focus() method tutorial for migration details.

Next: focusin event

Learn the bubbling focus event for parent forms and dynamic fields.

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

9 people found this page helpful