The ID selector matches one element with a given id attribute using #value syntax. Standard CSS since jQuery 1.0; jQuery uses document.getElementById() for bare ID lookups — the fastest selector path.
01
Syntax
#myDiv
02
Unique
One per page
03
Official
Red border
04
Escape
Dots, brackets
05
Combine
h2#title
06
0 or 1
Collection size
Fundamentals
Introduction
Every interactive page needs reliable hooks — a main container, a submit button, a modal root. The ID selector targets exactly one element when you assign a unique id attribute and select it with #idName in jQuery.
jQuery has supported ID selectors since version 1.0. Official documentation states that jQuery uses document.getElementById() for ID lookups, which is extremely efficient. The returned jQuery object contains zero or one element — chain .css(), .text(), or .on() just like any other collection.
Concept
Understanding the ID Selector
The hash prefix means “match this exact id attribute value”:
<div id="myDiv"> + $("#myDiv") → matches.
<div id="notMe"> + $("#myDiv") → no match.
Duplicate IDs in one document → invalid HTML; only the first node is found.
Missing ID → empty jQuery collection (.length === 0).
💡
Beginner Tip
IDs are for unique landmarks — header, root app node, one submit button. For repeating patterns (cards, rows), use classes (.item) instead of copying IDs.
Foundation
📝 Syntax
Official jQuery API form (since 1.0):
jQuery
jQuery( "#id" )
// typical usage:
$( "#myDiv" )
$( "#header" )
$( "h2#pageTitle" )
// escape special characters in id values:
$( "#myID\\.entry\\[1\\]" )
// scope descendants from an ID root:
$( "#sidebar .nav-link" )
Parameters
id — the value of the element’s id attribute, written after # without quotes in the selector string.
Return value
A jQuery object with zero or one DOM element.
Empty collection when no element has that id.
Official jQuery API examples
jQuery
// Example 1 — simple id:
$( "#myDiv" ).css( "border", "3px solid red" );
// Example 2 — escape dots and brackets in id:
$( "#myID\\.entry\\[1\\]" ).css( "border", "3px solid red" );
Cheat Sheet
⚡ Quick Reference
Goal
Selector
Notes
Select by id
$("#myDiv")
getElementById
Tag + id
$("h2#title")
Extra tag check
Escape . in id
#myID\\.entry
Backslash
Descendants
$("#app .btn")
Scope from root
Native JS
document.getElementById("myDiv")
Same target
Compare
📋 #id vs class, tag, and attribute selectors
Unique hooks vs repeating patterns vs tag-wide scans.
#id
$("#header")
One element
.class
$(".card")
Many matches
tag
$("div")
All tags
[id=]
[id="header"]
Attribute form
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery ID demos. Examples 3–5 cover tag+id combination, zero-or-one checks, and scoping descendants from an ID root.
📚 Official jQuery Demos
Border the target div — simple id and escaped special characters.
Example 1 — Official Demo: $("#myDiv")
Official jQuery demo — red border on #myDiv; sibling #notMe unchanged.
Legacy IDs — escape dots/brackets in auto-generated id values.
🧠 How jQuery Resolves #id
1
Parse selector
Detect bare #id or compound forms like h2#title.
Parse
2
getElementById
Bare ID path uses the browser’s fast native lookup — O(1) in practice.
Native
3
Verify compound
If a tag prefix exists, confirm the node name matches before accepting.
Check
4
✓
Wrap collection
Return jQuery object with 0 or 1 element — ready for chaining.
Important
📝 Notes
Available since jQuery 1.0 — standard CSS selector.
Each id must be unique per document — duplicate ids are invalid HTML.
Uses document.getElementById() for bare #id — fastest jQuery selector path.
Escape ., :, [, and other CSS-special chars in id values.
Compound tag#id adds a tag verification step after lookup.
Do not start id values with digits in HTML — invalid; use a letter prefix.
Compatibility
Browser Support
The ID selector is standard CSS and works in jQuery 1.0+ and native querySelector("#id") / getElementById in all modern browsers. Behavior is consistent because IDs are a core DOM feature.
✓ jQuery 1.0+ · standard CSS
jQuery ID Selector (#id)
Fastest selector path — one unique element per id value.
100%Universal
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
#idStandard
Bottom line: Use unique IDs for landmarks; classes for repeating patterns.
Wrap Up
Conclusion
The ID selector targets a single element by its id attribute with #value syntax. The official demos border #myDiv and show how to escape special characters in id strings.
Keep ids unique, check .length when an element may be missing, and use an ID root to scope descendant queries efficiently.
Confuse #id with [id="id"] needlessly on hot paths
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about #id
One unique hook per page.
5
Core concepts
#01
#id
One element
API
get02
getElementById
Fast path
Perf
\\03
Escape
Special chars
Tip
demo04
Official
#myDiv
Demo
0|105
.length
Zero or one
Rule
❓ Frequently Asked Questions
The ID selector matches the element whose id attribute equals the value after #. $("#myDiv") selects the element with id="myDiv". jQuery uses document.getElementById() for bare ID selectors — extremely efficient. Available since jQuery 1.0.
At most one. Calling jQuery("#id") returns a collection of zero or one DOM element. HTML requires unique IDs per document — duplicate IDs are invalid and only the first match is found.
Conceptually yes for a bare ID selector. Official jQuery docs state that getElementById() is used, which is very fast. jQuery wraps the result so you can chain .css(), .on(), and other methods.
Escape special CSS characters with backslashes in the selector string. Official demo: $("#myID\.entry\[1\]") matches id="myID.entry[1]".
Yes — h2#pageTitle adds a tag check. jQuery performs an extra verification after getElementById when another selector is attached to the ID.
Yes. ID selectors are core CSS and work in jQuery 1.0+ and native querySelector("#id"). Prefer unique, meaningful IDs for hooks and tests.
Did you know?
HTML5 allows almost any string as an id value, but CSS selectors treat characters like . and : specially. jQuery’s official second demo exists specifically to show backslash escaping — a common gotcha when frameworks auto-generate ids.