Javascript DOM How to - Get text inside parent div








Question

We would like to know how to get text inside parent div.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--from w w w.j a  v a2 s . c  o  m-->
<body>
  <div id="wrap">
    <h1>
      <a href=''>this is the text I want</a>
    </h1>
  </div>
  <script>
var element=document.getElementById("wrap");
console.log(element.innerText);
</script>
</body>
</html>

The code above is rendered as follows: