Javascript DOM How to - Get part of parent node's content








Question

We would like to know how to get part of parent node's content.

Answer


<!DOCTYPE html>
<html>
<body>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting 
  <a onclick="console.log(this.parentNode.innerHTML.substring(0, 20))" href="#">Some
      Link</a> industry.
  </p>
</body>
</html>

The code above is rendered as follows: