The :focus selector matches elements that currently have focus — the field being typed in, the button tabbed to, or the link activated. Standard CSS since jQuery 1.6; prefix with an element type for clarity.
01
Syntax
input:focus
02
Live state
Right now
03
Official
focus/blur
04
activeElement
Fast lookup
05
Prefix
Not bare :focus
06
CSS
Standard
Fundamentals
Introduction
Focus indicates which element is currently receiving keyboard input or pointer interaction. Highlighting the active field improves usability and accessibility. The CSS :focus pseudo-class — and jQuery’s matching selector — lets you style or query that live state in JavaScript.
jQuery supports :focus since version 1.6. Official documentation recommends prefixing it with a tag name or other selector; otherwise the universal selector is implied — $(":focus") searches like $("*:focus"). When you only need the one focused node, $(document.activeElement) is often faster.
Concept
Understanding the :focus Selector
Think of :focus as “is this element focused right now?”:
$("input:focus") → the input the user is currently editing (usually one).
Focus moves when the user tabs, clicks elsewhere, or scripts call .focus().
Only one element typically has focus at a time in a document.
Works in stylesheets and jQuery — e.g. a:focus { outline: 2px solid blue; }.
💡
Beginner Tip
To read the current focus target once, use $(document.activeElement). Use :focus inside event handlers or when filtering a subset — e.g. $("#form input:focus").
Foundation
📝 Syntax
Official jQuery API form (since 1.6):
jQuery
jQuery( ":focus" )
// typical usage — prefix with element:
$( "input:focus" )
$( "button:focus" )
$( "#contact-form :focus" )
// get current focus without scanning DOM:
$( document.activeElement )
Parameters
No arguments — matches elements that currently have focus among the preceding selector’s results.
Return value
A jQuery object containing zero or one focused element (typically one in the document).
Live focus state vs fast lookup vs editable form controls.
:focus
input:focus
Has focus now
activeElement
$(document.activeElement)
Direct lookup
:enabled
input:enabled
Can be edited
:disabled
input:disabled
Cannot focus
Hands-On
Examples Gallery
Example 1 follows the official jQuery delegate focus/blur demo. Examples 2–5 style focused inputs, use document.activeElement, scope to a form, and combine focusable types.
📚 Official jQuery Demo
Toggle a focused class on focus and blur inside #content.
Example 1 — Official Demo: delegate focus / blur
Official jQuery demo — focused elements get a blue background; blur removes it.
Tab or click into input, select, textarea, or button → .focused (blue bg)
Move focus away → class removed
setTimeout ensures :focus state is accurate after blur
How It Works
elem.is(":focus") checks live focus after the browser updates document.activeElement. The zero-delay timeout avoids a race between blur on the old element and focus on the new one.
Example 2 — Style Focused Input: input:focus
Highlight the active text field with a border and shadow.
Focused: INPUT email
Focused: TEXTAREA message
(No full DOM scan — direct reference)
How It Works
document.activeElement points directly at the focused node. Wrapping it in jQuery gives you the same chaining API without querying *:focus across the page.
Example 4 — Form Scope: #contact-form :focus
Check whether focus is inside a specific form before running validation UI.
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"
);
});
Tab through input, select, textarea, button → .is-active on focused control only
Matches standard form focusable elements
How It Works
Comma-separated selectors form a union — whichever type currently has focus matches. Disabled controls are skipped because they cannot receive focus.
Applications
🚀 Common Use Cases
Form UX — highlight input:focus for clearer field editing.
Keyboard nav — style button:focus and a:focus for accessibility.
Validation hints — show errors only on #form input:focus or after blur.
Focus tracking — official delegate pattern with elem.is(":focus").
Modal traps — detect when focus leaves a dialog via scoped :focus queries.
CSS parity — reuse :focus rules in stylesheets and jQuery.
🧠 How jQuery Evaluates :focus
1
Build candidate set
Evaluate the selector before :focus — e.g. all input elements or #form *.
Query
2
Check focus state
Keep elements where document.activeElement matches — live UI state, not markup.
Live
3
Return matches
Usually zero or one element — the control the user is interacting with now.
Set
4
✓
Chain methods
Apply .css(), .addClass(), or read values on the focused node.
Important
📝 Notes
Available since jQuery 1.6 — standard CSS pseudo-class.
Prefix with an element type — bare $(":focus") implies *:focus.
For the single global focus target, prefer $(document.activeElement).
Focus state changes on tab, click, and programmatic .focus() / .blur().
Works in native querySelectorAll("input:focus") in modern browsers.
Not the same as :focus-visible or :focus-within — different CSS pseudo-classes for other patterns.
Compatibility
Browser Support
The :focus pseudo-class is standard CSS and works in jQuery 1.6+. All modern browsers support document.querySelectorAll("button:focus") and document.activeElement. Same syntax in stylesheets and jQuery selectors.
Bottom line: Use input:focus or #form :focus — prefer document.activeElement for a single global lookup.
Wrap Up
Conclusion
The :focus selector matches elements that currently have focus — the official delegate demo toggles a focused class as users tab through controls inside #content.
Prefix your selector for clarity, use document.activeElement when you only need one node, and reuse the same pseudo-class in CSS and jQuery for consistent focus styling.
Use input:focus or #form :focus for scoped queries
Prefer $(document.activeElement) for one global lookup
Mirror focus styles in CSS and jQuery for consistency
Use setTimeout(..., 0) when toggling on blur/focus pairs
Ensure visible focus indicators for keyboard users
❌ Don’t
Scan with bare $(":focus") on large pages
Remove focus outlines without replacing them — hurts accessibility
Confuse :focus with :enabled or :checked — different states
Assume multiple elements have focus simultaneously
Forget disabled fields cannot receive focus
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :focus
Live focus state, not markup.
5
Core concepts
◎01
:focus
Live state
API
#02
Prefix
input:focus
Rule
ae03
activeElement
Fast path
Tip
demo04
Official
delegate
Demo
CSS05
Standard
Native CSS
CSS
❓ 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 — e.g. $("form input:focus").
Yes. :focus is standard CSS and works in native querySelectorAll when prefixed — e.g. document.querySelectorAll("a:focus"). jQuery has supported it since 1.6.
When focus moves from one element to another, blur and focus events fire in quick succession. A short setTimeout lets the browser update document.activeElement before elem.is(":focus") is evaluated — so the focused class toggles correctly.
Focusable elements include inputs, buttons, links, selects, textareas, and any element with tabindex. Disabled controls and purely decorative nodes typically cannot hold focus.
Did you know?
CSS also defines :focus-within (matches an ancestor when any descendant has focus) and :focus-visible (keyboard-style focus only). They complement :focus but use different matching rules — jQuery supports them as standard CSS selectors when prefixed with an element type.