DOM Document.getElementById() Method
The getElementById() method finds the element with a given id. It's the fastest way to get a single element.
Description
The getElementById() method returns the element whose id matches the string, or null if none does. IDs should be unique, so it returns a single element. It's the quickest lookup method, and unlike querySelector(), the ID doesn't need a # or escaping of special characters. It only exists on document (and document fragments), not on elements.
If more than one element mistakenly shares an ID, it returns the first in document order. The match is case-sensitive.
To find an element by ID within a fragment or template content, call getElementById() on that fragment; for any other element, use querySelector('#id'), escaping unusual characters with CSS.escape().
Syntax
document.getElementById(id)Parameters
- idThe ID of the element, without a
#.
Return Value
The element with that ID, or null if there is none.
Example
Browser Support
The following information will show you the current browser support for the Document.getElementById() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





