Sometimes, we want to append text to a div with JavaScript.
In this article, we’ll look at how to append text to a div with JavaScript.
How to append text to a div with JavaScript?
To append text to a div with JavaScript, we use the appendChild
method.
For instance, we write
const theDiv = document.getElementById("div");
const content = document.createTextNode("abc");
theDiv.appendChild(content);
to select the div with getElementById
.
Then we call createTextNode
with the text node’s text to create the text node.
Next, we call theDiv.appendChild
with content
to append context
as the last child node of the div.
Conclusion
To append text to a div with JavaScript, we use the appendChild
method.