We can create a HTML element programmatically using JavaScript.
For example, let’s say we have a body with just header in it.
<body>
<h1>Adding an element</h1>
</body>
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.
document.body.appendChild(element);
So, the body will become:
<body>
<h1>Adding an element</h1>
<p>Hello, World</p>
</body>
See it in action:
create elements examples html tags