Favorites

DOM Document.getElementById() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Document.getElementById()
DOM Document MethodThis method is supported by all modern browsers.
Updated 28th November 2026

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

<h2 id="title">Welcome</h2>
const title = document.getElementById('title');
console.log(title.textContent);
console.log(document.getElementById('missing'));
console.log(document.getElementById('title') === document.querySelector('#title'));

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee