How to Get an Element by Its ID with getElementById in JavaScript
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
document.getElementById(<id>)
| Part | What 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:

<!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 screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
getElementById('title')returned the heading, sotitle.textContentwasWelcome.getElementById('Title')returnednull. Ids are case-sensitive, and no element has the idTitle.- 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
| Mistake | What happens | Do this instead |
|---|---|---|
Adding a #, getElementById('#title') | null: the # belongs to CSS selectors, not to ids | Write 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 id | getElementById returns only the first one | Keep each id unique, and use a class for groups |
| A different case or spelling | null | Copy the id from the HTML |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Cannot set properties of null or Cannot read properties of null | No element with that id existed when the line ran | Check the spelling and case, and move the script below the element |
| The wrong element changes | Two elements share the id | Give 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
- Document: getElementById() method, on MDN.
- id global attribute, on MDN.
