How to Resolve "TypeError: getElementById is not a function" Error in JavaScript
The TypeError: document.getElementById is not a function is a common error in JavaScript that almost always points to one of two simple mistakes: a typographical error in the method name or an attempt to call the method on the wrong object.
This guide will explain the fundamental rules of the getElementById method, show you the common causes of this error, and provide the correct solutions.
The Core Problem: getElementById is a Method of document
The getElementById() method is a function that belongs exclusively to the global document object. Its purpose is to search the entire DOM for the single element that has a specific id.
The error is the JavaScript engine telling you, "You tried to call a function named getElementById, but it doesn't exist on the object you're using."
Cause 1 (Most Common): A Spelling or Capitalization Error
The getElementById method name is case-sensitive. A very common mistake is to miscapitalize one of the letters.
Example of problem:
// PROBLEM: The 'd' in 'Id' is capitalized, but it should be lowercase.
let myElement = document.getElementByID('my-id');
Error Output:
Uncaught TypeError: document.getElementByID is not a a function
Other common typos include getElementbyID or getelementbyid.
Solution: ensure the method is spelled correctly with its exact camelCase capitalization: getElementById.
// Correct: `Id` is all lowercase.
let myElement = document.getElementById('my-id');
Cause 2: Calling the Method on an Element Instead of document
The getElementById method is a unique selector because an id must be unique within the entire document. For this reason, the method only exists on the top-level document object. You cannot call it on another DOM element to search within its children.
Example of problem:
<div id="parent">
<p id="child">Some text</p>
</div>
let parentElement = document.getElementById('parent');
// PROBLEM: `getElementById` does not exist on `parentElement`.
let childElement = parentElement.getElementById('child');
Error Output:
Uncaught TypeError: parentElement.getElementById is not a function
Solution: always call getElementById on the global document object.
// Correct: Call the method on `document`.
let childElement = document.getElementById('child');
If you need to search for an element within another element, you must use a different method, such as querySelector().
let parentElement = document.getElementById('parent');
// Correct: Use `querySelector` to search within an element.
// Note that you must use a full CSS selector (including '#' for an ID).
let childElement = parentElement.querySelector('#child');
The Correct Way to Use getElementById
This example demonstrates the correct usage in a complete HTML file.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<div id="my-element">Hello, World!</div>
<!-- The script is placed at the end of the body -->
<script src="index.js"></script>
</body>
</html>
JavaScript (index.js):
// 1. Call the method on the `document` object.
// 2. Spell the method name correctly: `getElementById`.
// 3. Pass the ID string as an argument.
let element = document.getElementById('my-element');
if (element) {
element.style.color = 'blue';
}
Conclusion
The TypeError: getElementById is not a function is always caused by a simple mistake in how the method is called.
To solve it, ensure you are following these two rules:
- Check your spelling and capitalization: The method must be spelled exactly as
getElementById. - Check your object: The method must be called on the global
documentobject, not on any other DOM element.
By adhering to these two rules, you will resolve this common DOM manipulation error.