Javascript examples for DOM:Document getElementsByTagName
The getElementsByTagName() method returns elements with the specified tag name as a NodeList object.
The nodes can be accessed by index numbers. The index starts at 0.
Parameter | Type | Description |
---|---|---|
tagname | String | Required. The tagname of the elements you want to get |
A NodeList object, representing a collection of elements with the specified tag name.
The elements in the returned collection are sorted as they appear in the HTML code.
The following code shows how to Get all elements in the document with the specified tag name:
<!DOCTYPE html> <html> <body> <p>This is a p element</p> <p>This is also a p element.</p> <p>This is also a p element.</p> <button onclick="myFunction()">Test</button> <script> function myFunction() {// w w w. j a va 2 s .com var x = document.getElementsByTagName("P"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } } </script> </body> </html>