jQuery :target Selector

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is :target?

:target selects the element indicated by the fragment identifier (hash) in the document URI. Official docs: for https://example.com/#foo, $("p:target") selects the <p id="foo"> element.

Remember
URL: https://example.com/page#foo

$(":target")     → element with id="foo"
$("p:target")    → only if that element is a <p> (official style)
$("#foo")        → always finds #foo (hash does not matter)

// No hash → $(":target").length === 0
NeedUse
Current hash target$(":target")
Typed target$("p:target") / $("section:target")
Always by id$("#foo")
React to hash changes$(window).on("hashchange", …)
Document root$(":root")

How to Use :target

Give sections stable ids, link with #id, then style or read the current :target.

  1. Add an id to the element you want deep-linked (for example id="intro").
  2. Link to it with href="#intro" (or set location.hash).
  3. Query with $(":target") or a scoped form like $("section:target").
  4. Re-run the query on hashchange if you highlight with jQuery (CSS :target updates alone).
  5. Handle the empty case when the URL has no fragment.
PieceRequired?Role
URL fragmentYes (for a match)#id in the address bar
Matching idYesElement whose id equals the fragment
Tag prefixOptionalp:target only matches if the target is a p
Return value—0 or 1 element (empty if no hash / no id)

Pseudocode

Pseudocode
hash = URL fragment without "#"
if hash is empty:
    return empty set

target = element whose id == hash
if not found:
    return empty set

return jQuery(target)

// Optional: filter by tag → p:target, section:target, …

Cheat Sheet

Keep these facts handy while working with URL fragments.

TopicDetail
Syntax$(":target") / $("p:target")
SincejQuery 1.9 · standard CSS
MatchesElement whose id equals the URL hash
Count0 or 1
vs $("#id")Id always; :target only when hash matches
No hashEmpty collection
UpdatesOn navigation / hashchange
Native CSSdocument.querySelector(":target")

Live Preview

Jump to a section id below. The preview reads $(":target") for this page’s current hash.

Sample anchors

#target-demo-intro #target-demo-tips
Intro panel — id="target-demo-intro"
Tips panel — id="target-demo-tips"

Official idea: the fragment in the URI picks one element by id.

Result
hash: (none)
$(":target").length → 0

/* no fragment → empty set */

jQuery Examples

Short, copy-ready snippets. Open DevTools Console with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Read the Current :target

Set a hash, then show the target element’s id and text.

jQuery
location.hash = "foo";
var $t = $(":target");
$("#out").text(
  "id: " + ($t.attr("id") || "(none)") + "\\n" +
  "text: " + ($t.text() || "(empty)")
);
Try It Yourself

How It Works

  1. Set hash. location.hash = "foo" makes the fragment #foo.
  2. Select. $(":target") finds the element with id="foo".
  3. Read. .attr("id") and .text() describe that node.
  4. One match. At most one element is the URL target at a time.

Example 2 — :target vs $("#foo")

Id selectors always match; :target depends on the hash.

jQuery
location.hash = "";
console.log("no hash — :target", $(":target").length);
console.log("no hash — #foo", $("#foo").length);

location.hash = "foo";
console.log("with #foo — :target", $(":target").length);
console.log("with #foo — #foo", $("#foo").length);
Try It Yourself

How It Works

  1. Clear hash. :target is empty; $("#foo") still finds the node.
  2. Set hash. Both selectors match the same element.
  3. Meaning. Use id when you always need the node; use :target for “current deep link.”
  4. UI. Highlight active sections with :target, not a hard-coded id alone.

Example 3 — Official Style: p:target

Combine a tag with :target so only matching element types count.

jQuery
location.hash = "foo";
console.log("p:target →", $("p:target").length);
console.log("div:target →", $("div:target").length);
Try It Yourself

How It Works

  1. Hash. #foo points at <p id="foo">.
  2. Match tag. p:target succeeds because the target is a paragraph.
  3. Wrong tag. div:target fails even though an id match exists.
  4. Official. Docs use $("p:target") for this exact pattern.

Example 4 — Update on hashchange

Re-query :target whenever the fragment changes.

jQuery
function showTarget() {
  var $t = $(":target");
  $("#out").text($t.length ? "Active: #" + $t.attr("id") : "No target");
}
$(window).on("hashchange", showTarget);
showTarget();
Try It Yourself

How It Works

  1. Listen. hashchange fires when the fragment updates.
  2. Re-query. Call $(":target") again for the new active element.
  3. Display. Show the active id or a “no target” message.
  4. Initial. Run once on load for the starting hash.

Example 5 — Empty Set When There Is No Hash

Guard against a missing fragment before using the matched element.

jQuery
location.hash = "";
if ($(":target").length === 0) {
  $("#out").text("No fragment — nothing is :target");
} else {
  $("#out").text("Target: #" + $(":target").attr("id"));
}
Try It Yourself

How It Works

  1. Clear. An empty hash means no target element.
  2. Check length. 0 means do not call methods that need a node.
  3. Message. Tell the user (or skip highlight logic).
  4. Later. When a hash appears, the same selector starts matching.

Edge Cases & Pitfalls

Check these before building deep-link UI with :target.

empty

No hash → empty set

$(":target").length is 0 until a fragment is present and matches an id.

id

Missing id = no match

A hash like #missing matches nothing if no element has that id.

tag

Tag filter can reject a valid target

$("p:target") fails if the hashed element is a div. Use bare :target when any element type is fine.

version

Needs jQuery 1.9+

Older builds lack :target. Fall back to reading location.hash and $("#" + id).

Browser Support

The :target pseudo-class is standard CSS and has been in jQuery since 1.9. It works in native querySelector(":target") in all modern browsers.

✓ CSS · jQuery 1.9+

jQuery :target Selector

Supported in jQuery 1.9+ and every modern browser. Native: document.querySelector(":target") returns the element matching location.hash.

100% Standard CSS selector
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: Safe on current jQuery builds. Pair with hashchange (or CSS :target rules) for live deep-link highlighting.

Performance Notes

ApproachCostBest for
$(":target")CheapReading the active fragment target
document.querySelector(":target")CheapNative CSS without jQuery
$("#" + location.hash.slice(1))CheapFallback / older jQuery
CSS :target { … }CheapestHighlighting without JS

Because :target is standard CSS, jQuery can use the browser’s native engine. Cost is negligible — you resolve at most one element.

Key Takeaways

  • Role: select the element whose id matches the URL hash.
  • Not id alone: $("#foo") always matches; :target only when the hash is #foo.
  • Standard: works in native CSS; jQuery support since 1.9.
  • Live UI: re-query on hashchange, or style with CSS :target.

One line: $(":target") is the element named by the URL hash — empty with no fragment, perfect for deep-link highlighting.

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 example: $("p:target") selects a p with that id. Available since jQuery 1.9.
$("#foo") always finds the element with id foo. :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 (html in HTML). :target selects whichever element id matches the URL hash — often a section deep in the page, not the root.
No element matches :target — the matched set is empty. After the user follows a #section link or the hash changes, that element becomes :target.
Yes. Listen for hashchange, then re-query $(":target") or update styles. CSS :target rules also update automatically when the fragment changes.

Next: :input Selector

Learn how to select all form input-like elements with the :input selector.

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

9 people found this page helpful