
- 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 getElementsByTagName() Method
The HTML DOM getElementsByTagName() method is a read-only method used to retrieve a live HTMLCollection of all elements in the document with the specified tag name. If the specified tag does not exist in the document, it returns an empty HTMLCollection, not null.
If you pass an asterisk (*) as a parameter to this method, it will select all elements in the document and return an HTMLCollection containing all those elements.
The following interactive example demonstrate the usage of the getElementsByTagName() method for different scenarios −
Welcome to Tutorialspoint
You are at the right place to learn.... Visit for more
- If you click the "Change Content" button, it will change the content of the first paragraph to "Content changed to Tutorialspoint".
- If you click the "Add Style" button, it will apply styles to the second paragraph, changing the background color to "white" and the text color to "green".
Syntax
Following is the syntax of the HTML DOM getElementsByTagName() method −
document.getElementsByTagName(tagname);
Parameters
This method accepts a single parameter as listed below −
Parameter | Description |
---|---|
tagname | It represents the name of an element we want to retrieve. |
Return Value
It returns the HTMLCollection of found elements by the tagname specified.
Example 1
The following program demonstrates the usage of the HTML DOM getElementsByTagName() method −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementsByTagName() Method</title> <style> button{ padding: 8px 20px; } span{ position: relative; top: 10px; padding: 10px; } </style> </head> <body> <p>Click the below button to add style to the "span" element.</p> <button onclick="fun()">Click me</button> <br> <span id="type">You are at the right place to learn.</span> <script> function fun() { let x = document.getElementsByTagName("span"); x[0].innerHTML = "Welcome to Tutorials Point.."; x[0].style.color = "white"; x[0].style.backgroundColor = "#04af2f"; } </script> </body> </html>
The above program displays a "span" element with some styling −
Example 2: Change the Background color of Document
In this example, we will change the background color of the HTML document using the HTML DOM getElementsByTagName() method along with the "style" property −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementsByTagName() Method</title> <style> button{ padding: 8px 20px; } </style> </head> <body> <span id="type">You are at the right place to learn.</span> <p>You can go through our Tutorials.</p> <p>Click on the below button to change background-color of HTML Document</p> <button onclick="fun()">Click me</button> <br> <script> function fun() { let x = document.getElementsByTagName("body"); x[0].style.backgroundColor = "#04af2f"; x[0].style.color = "white"; } </script> </body> </html>
Once the program is executed, a button will be displayed. When clicked, the background color of the document will change to "green" −
Example 3: Get a List of all Tags
In the following example, we use the HTML DOM getElementsByTagName() method to retrieve all the elements by passing asterisk ("*") as a parameter and loop through it to display a list of all elements used in the HTML document −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementsByTagName() Method</title> <style> button{ padding: 8px 20px; } </style> </head> <body> <p>Click on the below button to get the list of all the Element used in this document:</p> <button onclick="fun()">Click me</button> <p id="list"></p> <script> function fun() { let x = document.getElementsByTagName("*"); let text = ""; for (let i = 0; i < x.length; i++) { text += x[i].tagName + "<br>"; } document.getElementById("list").innerHTML = "List of all element: <br>" + text; } </script> </body> </html>
On executing the above program, the list of all Elements will be displayed −
Example 4: Change the Content of <p> Element
In this example we will change the written text inside <p> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementsByTagName() Method</title> <style> button{ padding: 8px 20px; } </style> </head> <body> <span id="type"> You are at the right place to learn.</span> <p>Click the below button to change the content of the first paragraph (first p).</p> <button onclick="fun()">Click me</button><br> <p>I am Paragraph 1.</p> <p>I am Paragraph 2.</p> <script> function fun() { let x = document.getElementsByTagName("p"); x[0].innerHTML = "Welcome to Tutorials Point.."; } </script> </body> </html>
After executing the program, a button will appear. Clicking the button will change the content of a "p" element to "Welcome to Tutorials Point" −
Example 5: Get the Number of <p> Elements
The following example returns the number of <p> elements in the document −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getElementsByTagName() Method</title> </head> <body> <span id="type"> You are at the right place to learn.</span> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> <p id="num"></p> <script> function fun() { let x = document.getElementsByTagName("p").length; document.getElementById("num").innerHTML = "Total 'p' Elements: " + x; } fun(); </script> </body> </html>
Once the above program is executed, the count of the <p> element will be displayed −
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
getElementsByTagName() | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 5.1 |