jQuery API

ID Selector (“#id”)

id selector

version added: 1.0jQuery('#id')

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

Description: Selects a single element with the given id attribute.

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.

As always, remember that as a developer, your time is typically the most valuable resource. Do not focus on optimization of selector speed unless it is clear that performance needs to be improved.

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.

Examples:

Example: Finds the element with the id "myDiv".

<!DOCTYPE html>
<html>
<head>
  <style>
  div {
    width: 90px;
    height: 90px;
    float:left;
    padding: 5px;
    margin: 5px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div id="notMe"><p>id="notMe"</p></div>

  <div id="myDiv">id="myDiv"</div>
<script>$("#myDiv").css("border","3px solid red");</script>

</body>
</html>

Demo:

Example: Finds the element with the id "myID.entry[1]". See how certain characters must be escaped with backslashes.

<!DOCTYPE html>
<html>
<head>
  <style>
  div {
    width: 300px;
    float:left;
    padding: 2px;
    margin: 3px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <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>
<script>$("#myID\\.entry\\[1\\]").css("border","3px solid red");</script>

</body>
</html>

Demo:

Support and Contributions

Need help with ID Selector (“#id”) or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to ID Selector (“#id”)? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Hexodus

    What happens when the specifyed selector doesen’t exist?

  • http://blog.pakakuy.com/ Chris

    JQuery still returns almost as though it had found the object. The .length property will be 0 on failure to locate the ID.

  • http://www.learningjquery.com/ Karl Swedberg

    That's right. .value is a DOM property, not a jQuery property. If you want to use jQuery to get the value, use this:
    $(“#ReportsBasePath”).val();

  • pjam

    I was having the problem vamsipdhar and others reported.

    After a bit of research, I found out that this selector returns an object, and not an element as the description claims. Is this the expected behaviour? If so, the description is misleading.

    For those having the same prob as vamsipdhar and need a quick fix, try
    alert($(aClientID)[0].html());

    Hope someone knows how to answer my question.

    P.S. Im using jquery.1.3.2.min.js and also jqueryui-1.7.2

  • http://www.learningjquery.com/ Karl Swedberg

    The description does not claim that the selector returns an element; it says that it “selects” an element. Big difference. The selector itself doesn’t return anything. It’s merely used as an argument for the jQuery function.

  • Sir HelpSometimes.

    Like Karl said but I think I can help. In order to handle the jquery selector just like you would with getElementById() try $(“#elementYouWant”).get(0). Hope it helps.

  • http://nihongoresources.com Pomax

    How does one select certain elements under an id? say, <div id=”top”><input type=”radio”/><input type=”textfield”></div>, and the intention to only select any and all inputs of type “textfield” under #top?

  • Prakash

    Can I generate something like ${“#expand_${someElementId}”} for <img src=”expand_${someElementId}”> where ${someElementId} is iterative id in jstl?

  • Raj

    Is there a way we can get jQuery object from an array of DOM Element ID's ? something like

    $(elementIDs[]) ?

  • Emarketingdesk

    invalid argument jquery-1.3.2.min.js

    then in all solutions post solutions as

    step1) search return N.toUpperCase()}); in jquery-1.3.2.min.js
    step2) add if(K=='Infinitypx'){K=''};if(K=='NaNpx'){K=''}; after searched stmt;

    actually this was a bug in jquery-1.3.2.min.js where all the scnerios were not covered.

    adding this will sort out that error and it will work smoothly.

    Development Head
    Innovative Infosolutions
    http://www.innovativeinfosolutions.com

  • Chicna

    it return an array when i search with “#myId” ! it’s totally illogical ! by definition, an id is UNIQUE.

  • dknight

    That is for uniformity sake.
    ex:
    function(selector){
    var items = $(selector);
    //do something with items
    }

    for this example to work, it has to return an array in all cases.

  • http://el-creator.livejournal.com/ Artur

    Why this construction doesn't work?

    var number = 5; //Dummy, in real script it evaluates dynamically
    a = $(“#”+number).offset().left;//This causes an error $(divid).offset() is null (but really <div id=”5″> exists)</div>

  • Zev

    a foolish consistency is the hobgoblin of little minds

  • M Dint

    Yes, only the foolish consistency here is the insistance on unique id returns…

    Returning arrays makes perfect sense from an API standpoint…