$("#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.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("#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)
Call
What it does
Learn the jQuery ID selector: select one element by id with #id since 1.0, escape special characters, combine with tags, check .length, and scope descendants from a unique hook.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Add id="myDiv" to the target element (unique in the page).
Step 2
Call $("#myDiv") — jQuery uses getElementById.
Step 3
Chain methods: .text(), .on(), .hide(), and so on.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
Verify tag.h2#pageTitle keeps the element only if it is an h2.
Mismatch.div#pageTitle is empty when the node is an h2.
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.