innerHTML - How to get or change the inside of a HTML element

PHOTO EMBED

Sun Jan 26 2020 20:43:45 GMT+0000 (Coordinated Universal Time)

Saved by @_right_away_ #html #javascript #dom #dommanipulation

<html>	
	<div id="target">
    	<p>This is some text</p>
    </div>

  <script>

    //GET
      var divElement = document.getElementById("target").innerHTML;

    //CHANGE
      var heading = '<h1>Example text</h1>';
      document.getElementById("target").innerHTML = heading;

    //TO MAKE DIV EMPTY
        document.getElementById("target").innerHTML = '';

  </script>
  
</html>
content_copyCOPY

Line 10: GET contents of div element and store in variable divElement The value of the variable divElement is: <p>This is some text</p> Lines 13-14: The inside of the div element with id= "target" is replaced with a <h1> tag declared as variable heading. Line 17: The inside of the div element is set to empty. It now contains nothing.