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
Call
What it does
Selector prefix
Tag, id, or container — avoid bare :focus
Return value
jQuery set of currently focused matches (usually 0 or 1)
Since
jQuery 1.6
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Prefer a tagged or scoped selector ($("input:focus"), $("#form :focus")).
Step 2
For a one-off “what has focus?”, use $(document.activeElement).
Step 3
To style live focus, listen for focus/blur (or focusin/focusout).
Return Value
Return Value
Description
See examples
Check 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);
});
focusin. Bubbles, so one document listener is enough.
activeElement. Browser’s live focused node.
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.