javascript get text from div

Solutions on MaxInterview for javascript get text from div by the best coders in the world

showing results for - "javascript get text from div"
Irene
06 May 2017
1You'll probably want to try textContent instead of innerHTML.
2
3Given innerHTML will return DOM content as a String and not exclusively the "text" in the div. It's fine if you know that your div contains only text but not suitable if every use case. For those cases, you'll probably have to use textContent instead of innerHTML
4
5For example, considering the following markup:
6
7<div id="test">
8  Some <span class="foo">sample</span> text.
9</div>
10You'll get the following result:
11
12var node = document.getElementById('test'),
13
14htmlContent = node.innerHTML,
15// htmlContent = "Some <span class="foo">sample</span> text."
16
17textContent = node.textContent;
18// textContent = "Some sample text."