
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM getElementById() Method
The HTML DOM getElementById() method is used to retrieve an element by passing its id as a parameter to it. In HTML, the Id is an attribute used to specify a unique identifier for an element (e.g., <p id="demo">), where "demo" is an id value of <p> element.
If the id is not unique, only the first element with that id will be returned. If the specified id does not exist, this method will return null.
Before discussing the various examples of this method, let's consider an interactive part that will give you clarity about the various scenarios of this method:
Hey! This is an HTML paragraph
Syntax
Following is the syntax of the HTML DOM getElementById() method −
document.getElementById(ElementId);
Parameters
This method accepts a single parameter as listed below −
Parameter | Description |
---|---|
ElementId | It represents the id value of an element that you want to locate. It is a case-sensitive string. |
Return Value
This method returns the element of the given id, if no id exists with the specified id name it returns null.
Example 1
The following is the basic example of the HTML DOM getElementById() method −
<html> <head> <title>HTML DOM getElementById() Method</title> </head> <body> <p id="demo">Tutorialspoint</p> <script> alert(document.getElementById('demo')); </script> </body> </html>
A pop-up alert will appear with the message "[object HTMLParagraphElement]".
Example 2: Change font Color
We will use the HTML DOM getElementById() method to access an element by its 'id' first, and then we will change the font color using the "style" property −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementById() Method</title> </head> <body> <p>Click on the below button to change color.</p> <button onclick="fun()">Click me</button> <p id="tp">"Welcome to Tutorials Point.."</p> <script> function fun() { let x = document.getElementById("tp"); x.style.color = "red"; } </script> </body> </html>
Once the above program is executed, a button will be displayed. When clicked, the font color will change to "green".
Example 3: Display any Text
In the following example, we are displaying a text to <p> element.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementById() Method</title> </head> <body> <p>Click on the below to display a text</p> <button onclick="fun()">Click me</button> <p id="tp"></p> <script> function fun() { document.getElementById("tp").innerHTML = "Welcome to Tutorials Point"; } </script> </body> </html>
On executing the above program, a button will appear, once it is clicked, the text "Welcome to Tutorials Point" will be displayed.
Example 4: Change the Font Size
In the following example, the font size of the displayed text will be changed once the button is clicked −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementById() Method</title> </head> <body> <p>Click on the below button to change the font size.</p> <button onclick="fun()">Click me</button> <p id="tp">"Welcome to Tutorials Point.."</p> <script> function fun() { let x = document.getElementById("tp"); x.style.fontSize = "24px"; } </script> </body> </html>
After executing the above program, a button appears on the window screen, once it is clicked the font size will be changed to "24px".
Example 5: Add an EventListener
In the following example, we will access the button element using its id, and then we will add a click event using the addEvenetListener() method, which triggers an alert message −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementById() Method</title> </head> <body> <p>Click on the below button to alert a message </p> <button id="tp">click me</button> <script> document.getElementById("tp").addEventListener("click", fun); function fun() { alert("Welcome to Tutorialspoint"); } </script> </body> </html>
Once the above program is executed a button will be displayed, once it is clicked an alert message "Welcome to Tutorialspoint" will appear on the window screen.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
getElementByID | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 7 |