jQuery Focus Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Form Events

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("#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);
CallWhat it does
FocusLearn the jQuery focus event: bind with .on(\

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Bind with .on("focus", handler) on the element that should react.
Step 2Optionally pass eventData: .on("focus", data, handler).
Step 3Trigger with .trigger("focus") — only on visible elements.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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 →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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