Home >Web Front-end >JS Tutorial >How to Extract Text from a
Element Using Pure JavaScript" />
Problem:
When attempting to retrieve the text of a
Solution:
The issue arises because value is not an appropriate property for a
To illustrate, consider the following code:
<code class="javascript">function test() { var t = document.getElementById('superman').textContent; alert(t); }</code>
Compared to innerHTML, which returns the HTML content of the
For example, given the following HTML:
<code class="html"><div id="test"> Some <span class="foo">sample</span> text. </div></code>
The following results are obtained:
For more information, refer to the documentation on:
The above is the detailed content of How to Extract Text from a