document.createTextNode

Crée un nouveau nœud de texte.

Syntaxe

var text = document.createTextNode(données);
  • texte est un nœud de texte.
  • donnees est une chaîne contenant les données à placer dans le nœud de texte.

Exemple

<!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>

Spécifications

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

Compatibilité des navigateurs

BCD tables only load in the browser