jQuery :target Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
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
CallWhat it does
:targetLearn the jQuery :target selector: match the element pointed to by the URL hash, vs id selectors, hashchange updates, empty when no fragment, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Add an id to the element you want deep-linked (for example id="intro").
Step 2Link to it with href="#intro" (or set location.hash).
Step 3Query with $(":target") or a scoped form like $("section:target").

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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

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.

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 — Select Form Controls

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

:input Selector — Select Form Controls tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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