Document.createTextNode()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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

Sintaxis

js
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

html
<!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
# ref-for-dom-document-createtextnode①

Compatibilidad del Navegador

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
createTextNode

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support