jQuery :focus Selector — Match the Focused Element

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

What Is the :focus Selector?

:focus (since jQuery 1.6) selects the element that currently has focus — for example the input you are typing in. Like other : pseudo-classes, precede it with a tag or other selector when possible.

Remember
$("input:focus");                 // focused input
$("#form :focus");                 // focused control inside a form
$("input").is(":focus");           // true if that input has focus

// Prefer for a single lookup:
$(document.activeElement);

// Bare $(":focus") ≡ $("*:focus") — avoid
NeedUse
Filter focused inputs$("input:focus")
One focused element, fast$(document.activeElement)
React when focus changes.focus() / .focusin()
Enabled controls only:enabled
Visible elements:visible

How to Use :focus

Query on demand, or listen for focus/blur and test with .is(":focus").

  1. Prefer a tagged or scoped selector ($("input:focus"), $("#form :focus")).
  2. For a one-off “what has focus?”, use $(document.activeElement).
  3. To style live focus, listen for focus/blur (or focusin/focusout).
  4. After focus moves, a setTimeout(…, 0) can let .is(":focus") see the new state.
  5. Only focusable elements match — inputs, buttons, links, tabindex, and similar.
PieceRequired?Role
Selector prefixRecommendedTag, id, or container — avoid bare :focus
Return value—jQuery set of currently focused matches (usually 0 or 1)
Since—jQuery 1.6

Pseudocode

Pseudocode
call $(context).find(":focus")   // or $("input:focus")

for each candidate element in the query:
    if element is the document's focused node:
        include it

return the jQuery set

// Fast path (no DOM scan):
//   $(document.activeElement)

Cheat Sheet

Keep these facts handy while working with keyboard focus.

TopicDetail
Syntax$("tag:focus") · $("#box :focus")
SincejQuery 1.6
Bare form$(":focus") ≡ $("*:focus") — avoid
Test one element$el.is(":focus")
Fast lookup$(document.activeElement)
CSS siblingNative :focus / :focus-visible
Focusableinputs, buttons, links, select, textarea, tabindex
focus/blur racesetTimeout(fn, 0) before .is(":focus")

Live Preview

Tab or click the fields. The status line shows $("#preview-form :focus").

Check reports the focused field inside the form. Chips move focus for you.

Result
Ready — focus a field, then click Check.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official Focus/Blur Class Toggle

From the jQuery docs: toggle a focused class when controls inside #content gain or lose focus.

jQuery
$("#content").on("focus blur", "*", function () {
  var elem = $(this);
  setTimeout(function () {
    elem.toggleClass("focused", elem.is(":focus"));
  }, 0);
});
Try It Yourself

How It Works

  1. Delegate. focus/blur on any descendant of #content.
  2. Timeout. Lets the browser settle focus before testing.
  3. is(":focus"). True only for the element that currently has focus.
  4. toggleClass. Adds or removes focused from that boolean.

Example 2 — Style input:focus

On focus/blur, style the focused input and reset the others.

jQuery
$("input").on("focus blur", function () {
  $("input:focus").css({
    borderColor: "#2563eb",
    boxShadow: "0 0 0 3px rgba(37, 99, 235, 0.25)"
  });
  $("input").not(":focus").css({
    borderColor: "#cbd5e1",
    boxShadow: "none"
  });
});
Try It Yourself

How It Works

  1. Events. Both focus and blur refresh styles.
  2. input:focus. Selects only the active field.
  3. not(":focus"). Resets every other input.
  4. Tagged. Prefixed input: avoids a universal scan.

Example 3 — document.activeElement

Official recommendation: read the focused node directly — no full-tree :focus search.

jQuery
$(document).on("focusin", function () {
  var $focused = $(document.activeElement);
  $("#log").text(
    "Focused: " + $focused.prop("tagName") + " " +
    ($focused.attr("name") || $focused.attr("id") || "")
  );
});
Try It Yourself

How It Works

  1. focusin. Bubbles, so one document listener is enough.
  2. activeElement. Browser’s live focused node.
  3. Wrap. $(…) turns it into a jQuery object.
  4. Log. Shows tag and name/id without scanning the DOM.

Example 4 — Form Scope #contact-form :focus

Ask only for focus inside one form — ignores focus outside that container.

jQuery
$("#contact-form").on("focusin focusout", function () {
  var $inside = $("#contact-form :focus");
  $("#status").text(
    $inside.length
      ? "Editing: " + ($inside.attr("name") || "field")
      : "No field focused in form"
  );
});
Try It Yourself

How It Works

  1. Scope. Descendant :focus only under #contact-form.
  2. focusin/out. Bubble so the form hears child focus.
  3. length. Zero when focus leaves the form.
  4. name. Shows which field is being edited.

Example 5 — Multiple Focusable Types

Highlight whichever control has focus among inputs, selects, textareas, and buttons.

jQuery
var focusable = "input:focus, textarea:focus, select:focus, button:focus";

$(document).on("focusin", function () {
  $(focusable).addClass("is-active");
  $("input, textarea, select, button").not(":focus").removeClass("is-active");
});
Try It Yourself

How It Works

  1. Union. Several tagged :focus selectors in one string.
  2. Add. The focused control gets is-active.
  3. Clear. Non-focused siblings lose the class.
  4. Coverage. Works across common form control types.

Edge Cases & Pitfalls

CaseWhat happensFix
Bare $(":focus")Implied *:focus — wider scanPrefix with a tag or container
Test .is(":focus") in blur handlerMay still look focused brieflysetTimeout(fn, 0) before testing
Disabled inputCannot receive focusUse :enabled when relevant
Only need the focused nodeSelector search is unnecessary$(document.activeElement)
Non-focusable divNever matches :focusAdd tabindex if it should be focusable

Browser Support

:focus has been part of jQuery since 1.6. It mirrors the standard CSS :focus pseudo-class and works in every browser that runs your loaded jQuery version.

✓ Stable · jQuery 1.6+

jQuery :focus

Supported across jQuery 1.x (1.6+), 2.x, and 3.x. Prefer a current jQuery 3.x build. Use tagged selectors and activeElement for fast lookups.

100% With jQuery
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 Universal

Bottom line: Safe for form UX and keyboard focus checks. Prefer $("input:focus") or $(document.activeElement) over bare $(":focus").

Performance Notes

ApproachCostBest for
$(document.activeElement)CheapestSingle focused element lookup
$("input:focus") / scoped formCheapFiltering a known set
$(":focus") / $("*:focus")Wider scanAvoid in hot paths

Focus queries are usually tiny (0–1 matches). Cost comes from scanning too broadly — scope the selector, or skip the selector and read document.activeElement.

Key Takeaways

  • Role: match the currently focused element — since jQuery 1.6.
  • Prefer: $("input:focus") or $("#form :focus") over bare :focus.
  • Fast path: $(document.activeElement) for one node.
  • Race: use setTimeout(0) before .is(":focus") on blur.

One line: :focus finds the element that has keyboard focus — scope it, or use document.activeElement for a direct read.

Frequently Asked Questions

:focus selects elements that currently have keyboard or pointer focus. $("input:focus") matches the input the user is typing in right now. Available since jQuery 1.6.
Bare :focus implies the universal selector — equivalent to $("*:focus"), which searches every element type. Prefer $("input:focus"), $("button:focus"), or $("#form :focus") for clarity and performance.
Official jQuery docs: if you only need the single currently focused element, $(document.activeElement) retrieves it without searching the whole DOM tree. Use :focus when filtering a scoped collection — for example $("form input:focus").
Yes. :focus is standard CSS and works in native querySelectorAll when prefixed — for example document.querySelectorAll("a:focus"). jQuery has supported it since 1.6.
When focus moves from one element to another, blur and focus fire in quick succession. A short setTimeout lets the browser update document.activeElement before elem.is(":focus") runs — so a focused class toggles correctly.
Focusable elements include inputs, buttons, links, selects, textareas, and any element with a tabindex. Disabled controls and purely decorative nodes typically cannot hold focus.

Next: :empty Selector

Match elements that have no children — including text nodes.

:empty 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.

10 people found this page helpful