Javascript – how to view text inside DOM element

Javascript DOM method textContent can be used to get the text content inside a DOM. This can be useful to obtain content from a web page. Here is an example.

<div id="id1">
  <div>child1</div>
  <div>child2</div>
  <a href="/">link text</a>
</div>
<textarea id="ta1" rows="5">-</textarea>
<script type="text/javascript">
window.addEventListener("load", showText);
function showText() {
  var text = document.querySelector("#id1").textContent;
  document.querySelector("#ta1").value = text;
}
</script>
refresh

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments