Javascript DOM How to - Get nested div using document.getElementById








Question

We would like to know how to get nested div using document.getElementById.

Answer


<!DOCTYPE html>
<html>
<body>
<body>
  <div id="id1">
    <div id="id2"></div>
  </div><!--from ww  w.  j av a  2  s .co m-->
<script type='text/javascript'>
document.writeln(document.getElementById('id2'));
</script>
</body>
</body>
</html>

The code above is rendered as follows: