plainJS

Functions / Manipulation

Get the text content of an element

jQuery $.text()

2015 page

This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.

In short

Get the combined text contents of an element, including its descendants, or set the text content of the element.

Use the element property textContent (IE 8: innerText) for getting the content of an element and its descendants, stripped of all HTML tags:

js
var el = document.querySelector('div');
text = el.textContent || el.innerText;
console.log(text);