ID Selector ("#id")

Selects a single element with the given id attribute.

.id(id)

id StringAn ID to search for, specified via the id attribute of an element.

For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.

Calling jQuery() (or $()) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element.

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

If the id contains characters like periods or colons you have to escape those characters with backslashes.

Select the element with the id "myDiv" and give it a red border.

JS
<div id="notMe"><p>id="notMe"</p></div>
<div id="myDiv">id="myDiv"</div>
CSS
div {
  width: 90px;
  height: 90px;
  float: left;
  padding: 5px;
  margin: 5px;
  background-color: #eee;
}
HTML
$("#myDiv").css("border", "3px solid red");
DEMO

Select the element with the id "myID.entry[1]" and give it a red border. Note how certain characters must be escaped with backslashes.

JS
<div id="myID.entry[0]">id="myID.entry[0]"</div>
<div id="myID.entry[1]">id="myID.entry[1]"</div>
<div id="myID.entry[2]">id="myID.entry[2]"</div>
CSS
div {
  width: 300px;
  float: left;
  padding: 2px;
  margin: 3px;
  background-color: #eee;
}
HTML
$("#myID\\.entry\\[1\\]").css("border", "3px solid red");
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)