Javascript DOM How to - Get value of child div of a parent div








Question

We would like to know how to get value of child div of a parent div.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.addEvent('load', function() {
    var parent = document.getElementById("parent");
    var child = parent.children[0];
    var childval = child.innerHTML;
    document.getElementById("output").innerHTML=childval;
});<!--  w  ww.j a  v  a  2  s  .c o m-->
</script>
</head>
<body>
  <div id="parent">
    <div id="child">some-value</div>
  </div>
  <div id="output"></div>
</body>
</html>

The code above is rendered as follows: