: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
Call
What it does
:target
Learn 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
Parameter
Description
Step 1
Add an id to the element you want deep-linked (for example id="intro").
Step 2
Link to it with href="#intro" (or set location.hash).
Step 3
Query with $(":target") or a scoped form like $("section:target").
Return Value
Return Value
Description
See examples
Check 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.
p:target → 1
div:target → 0
(Target element is a paragraph)
How It Works
Hash.#foo points at <p id="foo">.
Match tag.p:target succeeds because the target is a paragraph.
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.