$("#id") (since jQuery 1.0) selects the single element whose id attribute equals that value. Bare ID lookups use document.getElementById() — extremely fast — and return a set of zero or one element.
Remember
$("#myDiv") // element with id="myDiv"
$("h2#pageTitle") // same id, must also be an h2
$("#myID\\.entry\\[1\\]") // escape . and [ ] in the id
$("#sidebar .item") // descendants under #sidebar
// Returns a jQuery object with 0 or 1 element
// Prefer unique, simple ids (letters, numbers, hyphens, underscores)
Put a unique id on the element, then select it with $("#…").
Add id="myDiv" to the target element (unique in the page).
Call $("#myDiv") — jQuery uses getElementById.
Chain methods: .text(), .on(), .hide(), and so on.
Check .length before assuming the element exists.
Escape ., [, ], :, etc. if the id contains them.
Piece
Required?
Role
# + id value
Yes
Match the id attribute
Tag prefix
No
Extra check — e.g. h2#pageTitle
Escapes
If needed
Backslash special CSS characters in the id
Return value
—
jQuery set with 0 or 1 element
Pseudocode
Pseudocode
call $("#idValue")
el = document.getElementById("idValue")
if el is null:
return empty jQuery set
else if selector has extra parts (e.g. h2#id):
if el also matches those parts:
return jQuery set with el
else:
return empty set
else:
return jQuery set with el
Reference
Cheat Sheet
Keep these facts handy for everyday ID lookups.
Topic
Detail
Syntax
$("#id")
Since
jQuery 1.0
Native path
document.getElementById()
Result size
0 or 1 element
CSS?
Yes — standard ID selector
Escape demo
$("#myID\\.entry\\[1\\]")
Duplicates
Invalid HTML — only first is selected
Try it
Live Preview
Enter an id (without #) and Run to see if $("#…") finds it.
myDiv
notMe
pageTitle
Result shows .length and the matched text (if any).
Result
Ready — enter an id and click Run.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.
Filter. Only .item.active under the sidebar matches.
Outside. The same classes in #main are ignored.
Edge Cases & Pitfalls
Small mistakes that make $("#…") miss the target.
unique
Duplicate ids are invalid
Only the first match is returned. Fix the HTML — do not depend on “first wins.”
escape
Dots and brackets need escapes
$("#a.b") means id a with class b. Use $("#a\\.b") for id a.b.
hash
Do not put # in the HTML id
Markup is id="myDiv"; the selector is $("#myDiv"). The hash is only in the selector.
missing
Missing id → empty set
No error is thrown. Check .length when presence matters.
class
Id ≠ class
$("#btn") is one unique node; $(".btn") can be many. Use the right hook.
Compatibility
Browser Support
The ID selector has been part of jQuery since 1.0 and maps to native document.getElementById(). It works in every browser that runs your loaded jQuery version.
✓ Stable · jQuery 1.0+
jQuery "#id"
Supported across jQuery 1.x, 2.x, and 3.x. Prefer a current jQuery 3.x build. Also available as native CSS querySelector("#id").
100%With jQuery
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
#idUniversal
Bottom line: The fastest everyday selector. Keep ids unique and simple; escape only when the id contains special characters.
Analysis
Performance Notes
Approach
Cost
Best for
$("#id")
Fastest
Single known element
$("#id .child")
Fast
Scoped searches under a unique root
$(".class") page-wide
More work
When many elements share a class
Official docs highlight that bare ID selectors use getElementById(). Prefer an id hook when you mean exactly one element.
Remember
Key Takeaways
Role: select one element by unique id — since jQuery 1.0.
Speed: bare $("#id") uses getElementById().
Size: always zero or one — check .length when needed.
Escape: backslash special characters in awkward ids.
One line:$("#id") is the fastest way to grab exactly one unique element.
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 $("#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 .text(), .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.