Javascript DOM How to - Target Child Elements in a ID








Question

We would like to know how to target Child Elements in a ID.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--   www.j av a2 s . c om-->
<body>
  <div id="title">
    <h1>Javascript Lesson 1</h1>
    <p>The basics</p>
  </div>
  <script type='text/javascript'>
var titleElement = document.getElementById("title");
var titleChildren = titleElement.getElementsByTagName("H1");
console.log(titleChildren);
console.log(titleChildren[0].innerHTML);

</script>
</body>
</html>

The code above is rendered as follows: