jQuery :focus Selector — Match the Focused Element

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

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

jQuery
$("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
CallWhat it does
Selector prefixTag, id, or container — avoid bare :focus
Return valuejQuery set of currently focused matches (usually 0 or 1)
SincejQuery 1.6

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

Parameters

ParameterDescription
Step 1Prefer a tagged or scoped selector ($("input:focus"), $("#form :focus")).
Step 2For a one-off “what has focus?”, use $(document.activeElement).
Step 3To style live focus, listen for focus/blur (or focusin/focusout).

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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.

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.

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 — Elements with No Children

Match elements that have no children — including text nodes.

:empty Selector — Elements with No Children 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.

10 people found this page helpful