jQuery ID Selector (#id)

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Standard CSS · jQuery 1.0+

What You’ll Learn

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

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.

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.

📝 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" );

⚡ Quick Reference

GoalSelectorNotes
Select by id$("#myDiv")getElementById
Tag + id$("h2#title")Extra tag check
Escape . in id#myID\\.entryBackslash
Descendants$("#app .btn")Scope from root
Native JSdocument.getElementById("myDiv")Same target

📋 #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

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.

jQuery
$( "#myDiv" ).css( "border", "3px solid red" );
Try It Yourself

How It Works

jQuery calls getElementById("myDiv") and wraps the result — at most one element matches.

Example 2 — Official Demo: escape id="myID.entry[1]"

IDs with dots and brackets must be escaped in the selector string.

jQuery
$( "#myID\\.entry\\[1\\]" ).css( "border", "3px solid red" );
Try It Yourself

How It Works

In CSS selectors, . and [ have special meaning — backslashes tell jQuery to match them literally in the id value.

📈 Practical Patterns

Tag verification, existence checks, scoped queries.

Example 3 — Tag + ID: h2#pageTitle

When another selector is attached to the id, jQuery verifies the tag name too.

jQuery
$( "h2#pageTitle" ).css( "color", "#2563eb" );
// div#pageTitle would NOT match — wrong tag
Try It Yourself

How It Works

Official docs: jQuery still uses getElementById, then performs an additional check that the node matches the tag prefix.

Example 4 — Zero or One: check .length

Safe pattern when an id might not exist yet in the DOM.

jQuery
var $el = $( "#missingPanel" );
if ( $el.length ) {
  $el.addClass( "active" );
} else {
  console.log( "Panel not found" );
}
Try It Yourself

How It Works

ID selectors never return more than one element — always test .length before assuming a match exists.

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

Use an ID as a fast root, then query descendants with space combinator.

jQuery
$( "#sidebar .item.active" ).css( "fontWeight", "bold" );
Try It Yourself

How It Works

getElementById finds the sidebar instantly; descendant selectors run only inside that subtree.

🚀 Common Use Cases

  • Page landmarks$("#header"), $("#footer"), $("#app").
  • Form submit — attach handlers to $("#signupForm").
  • Modals — show/hide $("#dialog") with one unique hook.
  • SPA roots — mount widgets under $("#root").
  • Scoped lists$("#cart .line-item") limits search area.
  • 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.

📝 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.

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 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 Standard

Bottom line: Use unique IDs for landmarks; classes for repeating patterns.

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.

💡 Best Practices

✅ Do

  • Use one unique id per landmark element
  • Prefer $("#app") for top-level hooks
  • Escape special characters in legacy id values
  • Check .length before acting on a match
  • Scope lists with $("#region .item")

❌ Don’t

  • Reuse the same id on multiple elements
  • Use ids for every list row — use classes instead
  • Forget backslashes for dots/brackets in ids
  • Assume $("#x") always finds something
  • Confuse #id with [id="id"] needlessly on hot paths

Key Takeaways

Knowledge Unlocked

Five things to remember about #id

One unique hook per page.

5
Core concepts
get 02

getElementById

Fast path

Perf
\\ 03

Escape

Special chars

Tip
demo 04

Official

#myDiv

Demo
0|1 05

.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.

Continue to Class Selector

After unique ID hooks, learn how .class selects many elements that share the same class name.

Class selector 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.

6 people found this page helpful