Skip to main content

How to Get an Element by Its ID with getElementById in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

document.getElementById('title') returns the element whose id attribute is exactly title, so your code can read or change it. The match is case-sensitive, and when no element has that id, you get null.

Syntax​

Syntax
document.getElementById(<id>)
PartWhat it does
<id>The element's id, as a string, written exactly as in the HTML and without a #.

It returns the element, or null when no element has that id. MDN's Document: getElementById() method page describes it.

The Example​

The example page has a heading with the id title. The script gets it with getElementById('title'), then asks for 'Title' with a capital T, and shows both results on the page.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a page with an h1 Welcome with the id title, an empty paragraph with the id result, and a script that gets title, tries to get Title with a capital T, and writes both results into the paragraph.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>getElementById</title>
</head>
<body>
<h1 id="title">Welcome</h1>
<p id="result"></p>

<script>
const title = document.getElementById('title');
const wrongCase = document.getElementById('Title');
document.getElementById('result').textContent =
'Found: ' + title.textContent + '. With a capital T: ' + wrongCase;
</script>
</body>
</html>

The last statement gets the paragraph by its id too, and puts a line of text into it with textContent.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a large Welcome heading, and below it Found: Welcome. With a capital T: null.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • getElementById('title') returned the heading, so title.textContent was Welcome.
  • getElementById('Title') returned null. Ids are case-sensitive, and no element has the id Title.
  • The script could find both elements because it comes after them in the page. The browser runs a script as soon as it reaches it.

Common Mistakes​

MistakeWhat happensDo this instead
Adding a #, getElementById('#title')null: the # belongs to CSS selectors, not to idsWrite the id alone: 'title'
Running the script before the element, in <head>getElementById returns null, and setting its text throws TypeError: Cannot set properties of null (setting 'textContent')Put the <script> at the end of <body>, after the elements
Giving two elements the same idgetElementById returns only the first oneKeep each id unique, and use a class for groups
A different case or spellingnullCopy the id from the HTML

Troubleshooting​

SymptomLikely causeFix
Cannot set properties of null or Cannot read properties of nullNo element with that id existed when the line ranCheck the spelling and case, and move the script below the element
The wrong element changesTwo elements share the idGive each element its own id

FAQ​

What's the difference between getElementById and querySelector?​

getElementById takes only an id, without #. querySelector takes any CSS selector, so document.querySelector('#title') finds the same heading. For a single id, either works.

Can an id contain spaces?​

No. An id is one word: HTML doesn't allow spaces in it.

Where is the id set?​

In the HTML, with the id attribute: <h1 id="title">. JavaScript can also give an element an id by setting its id property.

Conclusion​

document.getElementById is the direct way to get one element: pass its exact id, without #, and check for null. Most null results come from a typo, a different case, or a script that runs before the element exists.

Additional Resources​