Returns a reference to the element by its ID; the ID is a string which can be used to identify the element; it can be established using the id
attribute in HTML, or from script.
element = document.getElementById(id);
id
element
Element
object, or null
if an element with the specified ID is not in the document.<html> <head> <title>getElementById example</title> </head> <body> <p id="para">Some text here</p> <button onclick="changeColor('blue');">blue</button> <button onclick="changeColor('red');">red</button> </body> </html>
function changeColor(newColor) { var elem = document.getElementById('para'); elem.style.color = newColor; }
New users should note that the capitalization of 'Id' in the name of this method must be correct for the code to function; "getElementByID" does not work, however natural it may seem.
Unlike some other similar methods, getElementById
is only available as a method of the global document
object, and not available as a method on all element objects in the DOM. Because ID values must be unique throughout the entire document, there is no need for "local" versions of the function.
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id="parent-id"> <p>hello word1</p> <p id="test1">hello word2</p> <p >hello word3</p> <p>hello word4</p> </div> <script> var parentDOM = document.getElementById('parent-id'); var test1=parentDOM.getElementById('test1'); //throw error //Uncaught TypeError: parentDOM.getElementById is not a function </script> </body> </html>
If there is no element with the given id
, this function returns null
. Note that the id
parameter is case-sensitive, so document.getElementById("Main")
will return null
instead of the element <div id="main">
because "M" and "m" are different for the purposes of this method.
Elements not in the document are not searched by getElementById()
. When creating an element and assigning it an ID, you have to insert the element into the document tree with Node.insertBefore()
or a similar method before you can access it with getElementById()
:
var element = document.createElement('div'); element.id = 'testqq'; var el = document.getElementById('testqq'); // el will be null!
Non-HTML documents. The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "id" are not of type ID unless so defined in the document's DTD. The id
attribute is defined to be of ID type in the common cases of XHTML, XUL, and other. Implementations that do not know whether attributes are of type ID or not are expected to return null
.
Specification | Status | Comment |
---|---|---|
Document Object Model (DOM) Level 1 Specification The definition of 'getElementById' in that specification. | Recommendation | Initial definition for the interface |
Document Object Model (DOM) Level 2 Core Specification The definition of 'getElementById' in that specification. | Recommendation | Supersede DOM 1 |
Document Object Model (DOM) Level 3 Core Specification The definition of 'getElementById' in that specification. | Recommendation | Supersede DOM 2 |
DOM The definition of 'getElementById' in that specification. | Living Standard | Intend to supersede DOM 3 |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.7 or earlier) | 5.5 | 7.0 | 1.0 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.0) | 6.0 | 6.0 | 1.0 |
Document
reference for other methods and properties you can use to get references to elements in the document.Document.querySelector()
for selectors via queries like 'div.myclass'
getElementById()
to obtain 'xml:id' in XML documents (such as returned by Ajax calls)
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/getElementById