Element getElementsByTagName() Method - Javascript DOM

Javascript examples for DOM:Element getElementsByTagName

Description

The getElementsByTagName() method returns child elements by tag name as a NodeList object.

Parameter Values

Parameter Type Description
tagname String Required.

Return Value:

A NodeList object, representing child elements

The following code shows how to Change the background for selected element:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials