Document: createTextNode() method
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
<!doctype html>
<html lang="en">
<head>
<title>createTextNode example</title>
<script>
function addTextNode(text) {
const newText = document.createTextNode(text);
const 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>
Specifications
Specification |
---|
DOM Standard # ref-for-dom-document-createtextnode① |
Browser compatibility
BCD tables only load in the browser