Document.createTextNode()

Crea un nuevo nodo de texto. Este método puede ser usado para escapar caracteres HTML.

Sintaxis

var text = document.createTextNode(data);
  • text es un nodo Text.
  • data es una cadena de texto string que contiene los datos a poner en el nodo de texto.

Ejemplo

<!DOCTYPE html>
<html lang="en">
<head>
<title>createTextNode example</title>
<script>
function addTextNode(text) {
  var newtext = document.createTextNode(text),
      p1 = document.getElementById("p1");

  p1.appendChild(newtext);
}
</script>
</head>

<body>
  <button onclick="addTextNode('YES! ');">YES!</button>
  <button onclick="addTextNode('NO! ');">NO!</button>
  <button onclick="addTextNode('WE CAN! ');">WE CAN!</button>

  <hr />

  <p id="p1">First line of paragraph.</p>
</body>
</html>

Especificaciones

Specification
DOM Standard
# ref-for-dom-document-createtextnode①

Compatibilidad del Navegador

BCD tables only load in the browser