The Wayback Machine - https://web.archive.org/web/20250116000934/https://www.geeksforgeeks.org/html-dom-getelementbyid-method/
Open In App

HTML DOM getElementById() Method

Last Updated : 02 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The getElementById() method returns the elements that have given an ID which is passed to the function. This function is a widely used HTML DOM method in web designing to change the value of any particular element or get a particular element. If the passed ID to the function does not exist then it returns null.

Note: Each ID needs to be unique. If there are multiple elements with the same ID, only the first one will be returned.

Syntax:

document.getElementById( element_ID )

Parameter:

This function accepts single parameter element_ID which is used to hold the ID of the element.

Return Value:

It returns the object of the given ID. If no element exists with the given ID then it returns null.

Example 1: This example describes the getElementById() method where element_id is used to change the color of the text on clicking the button.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        DOM getElementById() Method
    </title>
    <script>
        // Function to change the color of element
        function geeks() {
            let demo = document.getElementById("geeks");
            demo.style.color = "green";
        }
    </script>
</head>

<body style="text-align:center">
    <h1 id="geeks">GeeksforGeeks</h1>
    <h2>DOM getElementById() Method</h2>

    <!-- Click on the button to change color -->
    <input type="button" onclick="geeks()" 
           value="Click here to change color" />
</body>

</html>

Output:

getElementById() Method

Example 2: This example describes the getElementById() method where the element_id is used to change the content on clicking the button.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        DOM getElementById() Method
    </title>
    <script>

        // Function to change content of element
        function geeks() {
            let demo = document.getElementById("geeks");
            demo.innerHTML = "Welcome to GeeksforGeeks!";
        }
    </script>
</head>

<body style="text-align:center">
    <h1>GeeksforGeeks</h1>
    <h2>DOM getElementById() Method</h2>
    <h3 id="geeks">Hello Geeks!</h3>

    <!-- Click here to change content -->
    <input type="button" 
           onclick="geeks()" 
           value="Click here to change content" />
</body>

</html>

Output:

getElementById() Method

Supported Browsers: The browser supported by DOM getElementById() method are listed below:

HTML DOM getElementById() Method – FAQs

Can getElementById() select multiple elements with the same ID?

No, IDs are supposed to be unique within a document. getElementById() only returns the first element with the specified ID. If IDs are duplicated, it returns the first matching element it encounters.

How does getElementById() handle case sensitivity?

The getElementById() method is case-sensitive. It will not match an ID if the case does not exactly match the ID used in the HTML.

Is getElementById() faster than other DOM selection methods?

Yes, getElementById() is generally faster than other selection methods like querySelector() or getElementsByClassName() because it directly accesses the ID-based lookup, which is highly optimized in browsers.

Can getElementById() be used on elements outside the document scope?

getElementById() is typically called on the document object. To search within a specific element, use querySelector() or getElementById() from a DocumentFragment or within a specific context.

What are common use cases for getElementById()?

Common use cases include accessing and manipulating specific elements by their IDs, such as changing text, updating styles, attaching event listeners, and interacting with forms.


Dive deep into the world of JavaScript with our JavaScript Full Course Online, offering everything from the basics to advanced techniques. Learn how to build interactive websites, optimize your code, and earn a certification to showcase your skills.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Push your limits and stay motivated as you work towards mastering JavaScript. Join now and get started on your JavaScript learning journey!


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg