We can create a HTML element programmatically using JavaScript.

For example, let's say we have a body with just header in it.

 <h1>Adding an element</h1>

Now, let's add paragraph in the body.

Step 1: Create an element with createElement() Method

var element = document.createElement('p');

Step 2: Add content in p tag using textContent property.

element.textContent = "Hello, World";

Step 3: Add the newly created element to the DOM.


So, the body will become:

 <h1>Adding an element</h1>
 <p>Hello, World</p>


Leave a reply

Your email address will not be published. required fields are marked *