Document: createTextNode() method
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.
Creates a new Text
node. This method can be used to escape HTML characters.
Syntax
js
createTextNode(data)
Parameters
data
-
A string containing the data to be put in the text node.
Return value
A Text
node.
Examples
html
<button>YES!</button> <button>NO!</button> <button>WE CAN!</button> <hr /> <p id="p1">First line of paragraph.</p>
js
function addTextNode(text) { const newText = document.createTextNode(text); const p1 = document.getElementById("p1"); p1.appendChild(newText); } document.querySelectorAll("button").forEach((button) => { button.addEventListener("click", (event) => { addTextNode(`${event.target.textContent} `); }); });
Specifications
Specification |
---|
DOM> # ref-for-dom-document-createtextnode①> |
Browser compatibility
Loading…