jQuery :focus Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Standard CSS · jQuery 1.6+

What You’ll Learn

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

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.

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").

📝 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).
  • Empty collection when nothing in scope has focus.

Official jQuery API example

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

⚡ Quick Reference

Goal:focusdocument.activeElement
Focused inputinput:focusCheck tag name after wrap
Focus inside a form#form :focusCompare IDs manually
Single global focus$(":focus") (slow)$(document.activeElement)
CSS stylesheetbutton:focusN/A (JS only)
Native CSSSupportedDOM API

📋 :focus vs document.activeElement and :enabled

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

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.

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

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.

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

input:focus narrows to the one input that currently has focus. Resetting non-focused inputs keeps styling consistent when tabbing between fields.

📈 Practical Patterns

Performance, scoping, and multiple focusable types.

Example 3 — Fast Lookup: document.activeElement

Official docs recommend this when you only need the currently focused element.

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

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"
  );
});
Try It Yourself

How It Works

Scoping with #contact-form limits which elements are considered — only focusable descendants of that form match :focus in this query.

Example 5 — Multiple Types: input:focus, textarea:focus, select:focus

Target every focusable form control type in one selector list.

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

Comma-separated selectors form a union — whichever type currently has focus matches. Disabled controls are skipped because they cannot receive focus.

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

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

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.

CSS · jQuery 1.6+

jQuery :focus Selector

Universal browser support. Native: querySelectorAll("input:focus").

100% Standard CSS
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: Use input:focus or #form :focus — prefer document.activeElement for a single global lookup.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :focus

Live focus state, not markup.

5
Core concepts
# 02

Prefix

input:focus

Rule
ae 03

activeElement

Fast path

Tip
demo 04

Official

delegate

Demo
CSS 05

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.

Continue to :empty Selector

After UI state filters, learn how :empty matches elements with no child nodes at all.

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

6 people found this page helpful