A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent below:

Node: textContent property - Web APIs

Node: textContent property

Baseline Widely available *

The textContent property of the Node interface represents the text content of the node and its descendants.

Note: textContent and HTMLElement.innerText are easily confused, but the two properties are different in important ways.

Value

A string, or null. Its value depends on the situation:

Warning: Setting textContent on a node removes all of the node's children and replaces them with a single text node with the given string value.

Differences from innerText

Don't get confused by the differences between Node.textContent and HTMLElement.innerText. Although the names seem similar, there are important differences:

Differences from innerHTML

Element.innerHTML returns HTML, as its name indicates. Sometimes people use innerHTML to retrieve or write text inside an element, but textContent has better performance because its value is not parsed as HTML.

Moreover, using textContent can prevent XSS attacks.

Examples

Start with this HTML fragment.

<div id="divA">This is <span>some</span> text!</div>

You can use textContent to get the element's text content:

let text = document.getElementById("divA").textContent;
// The text variable is now: 'This is some text!'

If you prefer to set the element's text content, you can do:

document.getElementById("divA").textContent = "This text is different!";
// The HTML for divA is now:
// <div id="divA">This text is different!</div>
Specifications Browser compatibility See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3