Get all elements in the document with the specified tag name:
var x = document.getElementsByTagName("LI");
Click the button to display the innerHTML of the second li element whose index is 1.
<!DOCTYPE html> <html> <body> <p>An unordered list:</p> <ul> <li>CSS</li> <li>HTML</li> <li>Java</li> </ul>/*from www . ja v a 2 s. co m*/ <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementsByTagName("LI"); document.getElementById("demo").innerHTML = x[1].innerHTML; } </script> </body> </html>
The getElementsByTagName()
method returns a collection of elements with the specified tag name, as an HTMLCollection object.
The HTMLCollection object represents a collection of nodes.
The nodes can be accessed by index numbers. The index starts at 0.
The parameter value "*" returns all elements in the document.
document.getElementsByTagName(tagname);
Parameter Values
Parameter | Type | Description |
---|---|---|
tagname | String | Required. The tagname of the elements you want to get |