How to Use getElementById in JavaScript

How to Use getElementById in JavaScript

As a JavaScript programmer, retrieving DOM elements is essential. On your path to learning JavaScript, manipulating the “Document Object Model” is unavoidable. Besides using jQuery or a framework, the most common methods for retrieving DOM elements in JavaScript are:

  1. getElementById()
  2. getElementsByTagName()
  3. querySelector()

Each of these methods has its pros and cons and the ideal situation in which you should use them. In today’s article, we’ll explore how to use getElementById in Javascript.

What is getElementById in JavaScript?

The getElementById() method is one of the most critical methods in the DOM. This method retrieves an element from an HTML document and returns its reference. The first key to how to use getElementById in JavaScript is understanding how this method functions.

Let’s start by going over some syntax and looking at an example.

JavaScript getElementById Syntax

Let’s review the syntax of this method:


As we can see, the syntax for the getElementById method is extremely simple. You can place this in your function to quickly target elements in your HTML file. You’ll need to give them an ID to target these elements effectively.

The ID is a case-sensitive string. Only one element may have any particular ID, so it is essential to give it a unique name that you can easily reference.

Have a look at the HTML code below to see where the ID value is placed in the element:

    <title>Example Code</title>
    <p id="change">This is sample text.</p>
    <button onclick="changeTextColor('green');">green</button>
    <button onclick="changeTextColor('purple');">purple</button>

Notice the ID “change,” attached to the <p> tag. We’ll target this ID in JavaScript to change the color of the text.

We’ll name our JavaScript function “changeTextColor” to keep it simple. As you can see from the JavaScript code below, we are retrieving the ID from the DOM using getElementById.

function changeTextColor(newColor) {
  const elem = document.getElementById('change'); = newTextColor;

Let’s break this down a little further.

We first created our function. Then we created the const variable “elem” and assigned it a value of document.getElementByID, and placed the parameter ‘change’ to target the corresponding ID in the DOM.

Now, with the onClick event tied to the buttons in our DOM, the function will execute, changing the text color of the corresponding ID with the addition of a little bit of CSS.

Does getElementById Work With All HTML Attributes?

All HTML elements support ID attributes. Therefore, you are able to target and retrieve any HTML element by simply adding an ID attribute.

You may target paragraphs, headings, buttons, and even <a> tags. But that’s not all: Any element that can support an ID attribute can be retrieved with the getElementById method.

What if an Element does not Exist?

Luckily, JavaScript is built in a way that it won’t cause any harm if your function doesn’t work. Sure, you might run into some frustrating bugs, but nothing catastrophic will happen.

If an element with the corresponding ID you are trying to target does not exist, the getElementById method will return null.

This can happen if your ID name does not match perfectly. You need to ensure that your spelling and capitalization are an exact match. Otherwise, your method will not know which element to retrieve.

Does getElementById Work Across All Web Browsers?

Fortunately, the getElementById method is one of JavaScript’s most widely supported DOM retrieval methods. This method can be used across all major web browsers, Chrome, Safari, Firefox, Edge, Brave, and Opera. It even has no trouble with mobile browsers. Thanks to this wide support, you can feel free to use getElementById without needing to worry about browser incompatibility.

Final Thoughts

There are a few methods for retrieving elements in the DOM, but the getElementById method will prove invaluable to any dev. If you’re wondering how to learn JavaScript fast, you’ll need a solid foundation in fundamentals such as this. With that being said, the getElementById method in JavaScript is one of the most commonly used methods for beginner and advanced projects alike.

If you have any questions or need help on your journey, feel free to leave a comment below!

Sources for this post: