jQuery ID Selector ("#id")

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

$("#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)
CallWhat 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

ParameterDescription
Step 1Add id="myDiv" to the target element (unique in the page).
Step 2Call $("#myDiv") — jQuery uses getElementById.
Step 3Chain methods: .text(), .on(), .hide(), and so on.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official Demo: $("#myDiv")

Select the element with id="myDiv" and mark it.

jQuery
$("#myDiv").append(" [selected]");
Try It Yourself

How It Works

  1. Lookup. $("#myDiv") calls getElementById.
  2. One match. Only the element with that exact id is returned.
  3. Chain. .append() runs on that single element.

Example 2 — Escape Special Characters in an Id

Official pattern: escape . and [ ] with backslashes.

jQuery
$("#myID\\.entry\\[1\\]").append(" [selected]");
Try It Yourself

How It Works

  1. Id value. The real attribute is myID.entry[1].
  2. CSS meaning. Unescaped . would mean “class” in a selector.
  3. Escape. \\. and \\[ \\] in the JS string pass literal characters to Sizzle/CSS.

Example 3 — Tag + ID: h2#pageTitle

Attached selectors add a verification step after the id lookup.

jQuery
$("h2#pageTitle").append(" [h2 match]");
$("#log").text(
  "h2#pageTitle → " + $("h2#pageTitle").length +
  " | div#pageTitle → " + $("div#pageTitle").length
);
Try It Yourself

How It Works

  1. Find by id. jQuery still starts with the id.
  2. Verify tag. h2#pageTitle keeps the element only if it is an h2.
  3. 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.

Next: Class Selector (".class")

Select every element that shares a class — the many-element counterpart to #id.

Class Selector (".class") 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.

10 people found this page helpful