jQuery :target Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
URL hash

What You’ll Learn

The :target selector matches the element whose id equals the URL hash fragment — e.g. #foo highlights the element with id="foo". Available since jQuery 1.9; standard CSS pseudo-class.

01

Syntax

:target

02

#hash

Fragment

03

Deep link

Jump to id

04

vs #id

Hash-aware

05

CSS

Standard

06

hashchange

Live update

Introduction

Long documentation pages, FAQs, and wikis use in-page anchor links like <a href="#pricing"> so readers jump straight to a section. The browser updates the URL hash, and the element with matching id becomes the current target.

jQuery’s :target pseudo-class (since 1.9) selects that hash-referenced element. Official documentation example: for URI https://example.com/#foo, the expression $("p:target") selects the <p id="foo"> element. The same pseudo-class works in CSS stylesheets without JavaScript.

Understanding the :target Selector

Think of :target as “which section does the URL point at right now?”:

  • page.html#about#about element matches :target.
  • page.html (no hash) → nothing matches :target.
  • $(":target") → any element type with matching id.
  • $("section:target") → only if the targeted node is a section.
💡
Beginner Tip

Every hash link needs a matching id on the destination element. Without it, the URL changes but :target matches nothing.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":target" )

// any targeted element:
$( ":target" )

// scoped (official docs example for #foo on a p):
$( "p:target" )
$( "section:target" )

// read current hash:
window.location.hash   // "#foo"

// native CSS (standard):
document.querySelector( ":target" )

// CSS stylesheet (no jQuery):
// :target { background: #fef3c7; }

Parameters

  • No arguments — matches the element whose id equals the URI fragment (without #).

Return value

  • A jQuery object containing zero or one element — at most one id matches the single hash.

Official jQuery API example

jQuery
// URI: https://example.com/#foo
$( "p:target" );   // selects <p id="foo">…</p>

// Highlight whichever section the hash references:
$( ":target" ).addClass( "is-highlighted" );

⚡ Quick Reference

URL:target$("#id")
page.html#foo#foo if existsAlways #foo
page.html (no hash)Empty setStill finds #foo
p:targetOnly if target is pN/A
CSS :target { }Auto on hash changeNeeds JS or class
querySelector(":target")Supported (standard CSS)querySelector("#foo")

📋 :target vs #id and :root

Hash-aware selection vs always-on id lookup vs document root.

:target
:target

Matches URL hash

#id
$("#foo")

Always that id

:root
:root

Document root

CSS
:target { … }

No JS needed

Examples Gallery

Examples follow the official jQuery :target definition for URI fragments. Try the labs with different hash values in the URL bar to see selection change live.

📚 Hash Fragment Basics

Style the element the URL points at.

Example 1 — Highlight :target on Load

When the URL ends with #about, add a highlight class to the targeted element.

jQuery
$( ":target" ).addClass( "is-highlighted" );

// With URI …/page.html#about → #about gets the class
// With no hash → matched set is empty
Try It Yourself

How It Works

Only the element whose id matches the fragment is in the matched set — perfect for deep-link emphasis.

Example 2 — Official Pattern: p:target

Official jQuery docs — scope :target to paragraph elements only.

jQuery
// URI: https://example.com/#foo
var el = $( "p:target" );

if ( el.length ) {
  $( "#log" ).text( "Targeted paragraph: " + el.text() );
}
Try It Yourself

How It Works

Combining a tag name with :target filters by element type — both id and tag must match.

📈 Practical Patterns

Hash reading, pure CSS, and live updates.

Example 3 — Compare :target with location.hash

Verify the targeted element id matches the current fragment.

jQuery
var hash = window.location.hash;          // "#pricing"
var target = $( ":target" );

console.log( "Hash:", hash );
console.log( "Target id:", target.attr( "id" ) );
Try It Yourself

How It Works

location.hash includes the #; element id attributes do not.

Example 4 — Pure CSS :target { } Styling

Standard CSS applies highlight styles without jQuery — same pseudo-class as jQuery :target.

jQuery
section:target {
  background: #fef3c7;
  border-left: 4px solid #f59e0b;
  padding-left: 12px;
}

/* jQuery equivalent on load: */
/* $( "section:target" ).addClass( "highlight" ); */
Try It Yourself

How It Works

Because :target is standard CSS, stylesheets and jQuery queries refer to the same browser concept.

Example 5 — Update on hashchange

Re-apply highlight when the user clicks another in-page anchor link.

jQuery
function highlightTarget() {
  $( "section" ).removeClass( "active" );
  $( ":target" ).addClass( "active" );
}

highlightTarget();
$( window ).on( "hashchange", highlightTarget );
Try It Yourself

How It Works

CSS :target updates automatically; jQuery needs hashchange (or similar) to re-run class toggles on navigation.

🚀 Common Use Cases

  • Documentation TOCs — highlight the section linked from the table of contents.
  • FAQ accordions — open or emphasize the panel matching #question-3.
  • Tab-less nav — style section:target as the active panel.
  • Shareable links — send URLs with hashes so recipients land on the right paragraph.
  • CSS-only UX — use :target { } for lightbox or reveal patterns.
  • Analytics — log $( ":target" ).attr( "id" ) on hashchange.

🧠 How jQuery Evaluates :target

1

Read URI fragment

Browser exposes the hash — e.g. #foo from location.hash.

Hash
2

Find id match

Look for an element whose id attribute equals the fragment (without #).

Lookup
3

Apply tag filter

If selector is p:target, keep the match only when the node is a paragraph.

Scope
4

Return collection

Zero or one element — chain .addClass(), read text, or animate scroll.

📝 Notes

  • Available since jQuery 1.9 — matches URI fragment target element.
  • Standard CSS — works in querySelector(":target") and stylesheets.
  • At most one element matches — one hash fragment at a time.
  • No hash in URL → empty jQuery collection.
  • Fragment ids must be unique — duplicate ids produce undefined behavior.
  • Do not confuse with jQuery .target event property or :root document root.

Browser Support

The :target pseudo-class is standard CSS and works in jQuery 1.9+. Native document.querySelector(":target") and CSS :target { } rules are supported in all modern browsers.

jQuery 1.9+ · standard CSS

jQuery :target Selector

URL hash fragment → element with matching id.

100% jQuery + native 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
:target Universal

Bottom line: Pair anchor links with matching ids; CSS :target works without JavaScript.

Conclusion

The :target selector matches the element referenced by the URL hash — official docs example: URI #foo and $("p:target") for <p id="foo">.

Use it for deep-link highlights, or pure CSS :target rules when JavaScript is not needed.

💡 Best Practices

✅ Do

  • Give every hash link a matching unique id
  • Use CSS :target for simple highlight effects
  • Listen for hashchange when toggling jQuery classes
  • Scope with section:target when needed
  • Prefer semantic section ids (#pricing, #faq)

❌ Don’t

  • Confuse :target with $("#id") — hash matters
  • Expect :target to match without a hash in the URL
  • Duplicate ids on one page
  • Forget URL-encoded characters in fragment ids
  • Assume :target works for query strings — only the hash

Key Takeaways

Knowledge Unlocked

Five things to remember about :target

Hash → id match.

5
Core concepts
0–1 02

One max

Per hash

Rule
p: 03

Scoped

Tag filter

Tip
CSS 04

Standard

Native OK

Compare
Δ 05

hashchange

Re-query

Demo

❓ Frequently Asked Questions

:target selects the element indicated by the fragment identifier (hash) in the document URI. If the URL is https://example.com/#foo, the element with id="foo" matches. Official jQuery docs example: $("p:target") selects a p element with that id. Available since jQuery 1.9.
$("#foo") always finds the element with id foo regardless of the URL hash. :target matches only when the current URL fragment equals that id — it reflects which section the user navigated to via the hash.
Yes. :target is defined in CSS Selectors and works in native document.querySelector(":target"). jQuery added support in 1.9 to align with the standard pseudo-class.
:root selects the document root element (html in HTML pages). :target selects whichever element id matches the URL hash fragment — often a section deep in the page, not the root.
No element matches :target — the matched set is empty. Once the user clicks a link to #section or the hash changes, the corresponding element becomes :target.
Yes. Listen for the hashchange event (or popstate in some SPA setups), then re-query $(":target") or apply styles. CSS :target rules also update automatically when the fragment changes.
Did you know?

CSS-only lightbox patterns use :target on hidden panels — when the URL becomes #modal, the #modal element matches :target and CSS can display it with :target { display: block; }. jQuery can enhance the same ids with animations, but the pseudo-class works without any script.

Continue to :input Selector

After URL hash targeting, learn how :input matches every form control in one expression.

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