Javascript DOM How to - Get inner html from div and pre element








Question

We would like to know how to get inner html from div and pre element.

Answer


<html>
<body>
  <div id="mydiv">hello div world</div>
  <pre id="mypre">
hello<!-- www.ja v  a  2 s . c  om-->
pre
world
</pre>
  <script>
console.log("div content: "+document.getElementById("mydiv").innerHTML)
console.log("pre content: "+document.getElementById("mypre").innerHTML)
</script>
</body>
</html>

The code above is rendered as follows: