jQuery ID Selector ("#id")

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is the #id Selector?

$("#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)
NeedUse
One unique element$("#myDiv")
Many by class$(".item")
All of a tag$("div")
Several selectors$("#a, #b")

How to Use #id

Put a unique id on the element, then select it with $("#…").

  1. Add id="myDiv" to the target element (unique in the page).
  2. Call $("#myDiv") — jQuery uses getElementById.
  3. Chain methods: .text(), .on(), .hide(), and so on.
  4. Check .length before assuming the element exists.
  5. Escape ., [, ], :, etc. if the id contains them.
PieceRequired?Role
# + id valueYesMatch the id attribute
Tag prefixNoExtra check — e.g. h2#pageTitle
EscapesIf neededBackslash 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

Cheat Sheet

Keep these facts handy for everyday ID lookups.

TopicDetail
Syntax$("#id")
SincejQuery 1.0
Native pathdocument.getElementById()
Result size0 or 1 element
CSS?Yes — standard ID selector
Escape demo$("#myID\\.entry\\[1\\]")
DuplicatesInvalid HTML — only first is selected

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.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

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.
  4. Other ids. #notMe is untouched.

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.
  4. Tip. Prefer simple ids so you rarely need escapes.

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.
  4. Bare id. $("#pageTitle") would match regardless of tag.

Example 4 — Zero or One: Check .length

Always guard before acting on a possibly missing id.

jQuery
var lines = [];
var $found = $("#existingPanel");
if ($found.length) {
  $found.append(" [found]");
  lines.push("existingPanel → found");
}
var $missing = $("#missingPanel");
lines.push(
  $missing.length
    ? "missingPanel → found"
    : "missingPanel → not found (.length 0)"
);
$("#log").text(lines.join("\n"));
Try It Yourself

How It Works

  1. Found. .length is 1 when the id exists.
  2. Missing. Unknown ids return an empty set (.length is 0).
  3. Safe. Methods on an empty set are no-ops — still check when logic depends on presence.
  4. Rule. ID selectors never return more than one element.

Example 5 — Scope: $("#sidebar .item")

Use an id as a root, then select descendants under it only.

jQuery
$("#sidebar .item.active").append(" [sidebar]");
Try It Yourself

How It Works

  1. Root. #sidebar is the unique container.
  2. Descendants. Space means “inside that container.”
  3. Filter. Only .item.active under the sidebar matches.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
#id Universal

Bottom line: The fastest everyday selector. Keep ids unique and simple; escape only when the id contains special characters.

Performance Notes

ApproachCostBest for
$("#id")FastestSingle known element
$("#id .child")FastScoped searches under a unique root
$(".class") page-wideMore workWhen many elements share a class

Official docs highlight that bare ID selectors use getElementById(). Prefer an id hook when you mean exactly one element.

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.

Next: Class Selector

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

.class tutorial →

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