Javascript DOM How to - Change inner html with Javascript








Question

We would like to know how to change inner html with Javascript.

Answer


<!DOCTYPE html>
<html>
<head>
<style>
<!--from w  ww  .ja v a 2  s  .  c o m-->
.dash {
  border: 5px dashed #ccc;
  padding: 100px;
  display: block;
}
</style>
</head>
<body>
  <div id="hello"  class="dash">HELLO WORLD!</div>

  <script>
        if (document.getElementById('hello')) {
          document.getElementById('hello').innerHTML = 'Hello World - this was inserted using JavaScript';
        }
    </script>
</body>
</html>

The code above is rendered as follows: