VOOZH about

URL: https://www.geeksforgeeks.org/html/html-dom-getelementbyid-method/

⇱ HTML DOM getElementById() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HTML DOM getElementById() Method

Last Updated : 28 Jul, 2025

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.

Output:

👁 Image
getElementById() Method

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

Output:

👁 Image
getElementById() Method

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

Comment
Article Tags: