Table of Contents

<HTMLElement>.setText()

Sets the text content of the Element it is called on, possibly overriding any existing text or element nodes.

Usage:
HTMLElement = Element.setText(text)
Member of:
HTMLElement
Parameters:
text – the text content to set (required).
Returns:
The HTMLElement it was called on, thus allowing for command chaining.
Notes:
  • This function encodes HTML special characters (<, >, &, " and ') before creating the text node, thus HTML code passed as parameter will be displayed as text rather than parsed into a DOM structure. If you want to parse HTML code, please use the .setHtml() function instead.

Examples

Simple example:

let foo = document.getElementById('foo');
foo.setText('Hello, world!');

See also

More information