Javascript examples for DOM:Element getElementsByTagName
The getElementsByTagName() method returns child elements by tag name as a NodeList object.
Parameter | Type | Description |
---|---|---|
tagname | String | Required. |
A NodeList object, representing child elements
The following code shows how to Change the background for selected element:
<!DOCTYPE html> <html> <head> <style> #myDIV {/*from w w w .ja v a 2 s .com*/ border: 1px solid black; margin: 5px; } </style> </head> <body> <div id="myDIV"> <h3>A h3 element in div.</h3> <p>P element in div.</p> <span>A span element in div.</span> <h2>A h2 element in div.</h2> <div>A div element in div.</div> <p>Another p element in div.</p> </div> <button onclick="myFunction()">Test</button> <script> function myFunction() { var x = document.getElementById("myDIV"); x.getElementsByTagName("*")[3].style.backgroundColor = "red"; } </script> </body> </html>