How to Write HTML with JavaScript?

Estimated read time 1 min read

You can write HTML elements dynamically in JavaScript by creating HTML elements using JavaScript and then adding them to the HTML DOM.

Here’s an example:

// Create a new div element
let newDiv = document.createElement("div");

// Create a new text node
let newContent = document.createTextNode("Hello World!");

// Add the text node to the div

// Add the div to an existing element on the page
let parent = document.getElementById("parentId");

In this example, a new div element is created using document.createElement("div"). Then, a new text node is created using document.createTextNode("Hello World!"). The text node is added to the div using newDiv.appendChild(newContent). Finally, the div is added to an existing element on the page using parent.appendChild(newDiv).

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply