Javascript DOM How to - Modify inner html for a div element








Question

We would like to know how to modify inner html for a div element.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.addEvent('load', function() {
element = document.getElementById('abc');
element.innerHTML = "<div class='a'>Some other contents here </div>" + element.innerHTML;
});<!--  www .ja  v a 2 s . c o  m-->
</script>
</head>
<body>
  <div id='abc'>
    <div class='a'>Some contents here</div>
    <div class='b'>Some other contents</div>
  </div>
</body>
</html>

The code above is rendered as follows: