<!DOCTYPE HTML>
<html>
<head>
<title>
How to dynamically create
new elements in JavaScript?
</title>
</head>
<body>
<h1 style="text-align:center; color:green;">
GeeksForGeeks
</h1>
<!-- Form to add item -->
<form action="#" style="text-align:center;">
<!-- Type of Element -->
<label for="type">
Add Element Type
</label>
<input type="text" id="type"
placeholder="Like: div, h1, li...."
value="li" />
<br /><br />
<!-- Text/Value for the element --->
<label for="value">
Add Element Value
</label>
<input type="text" id="value"
placeholder="Like: Hello GeeksForGeeks"
value="CHILD 2" />
<br /><br />
<!-- Submit the Form -->
<button type="button"
onClick="addItem()">
Add
</button>
</form>
<!-- Parent tag where we add
item as child -->
<ol id="parent">
<li>List Item 1</li>
</ol>
<script>
function addItem() {
let type = document.
getElementById("type").value;
let value = document.
getElementById("value").value;
type
= document.createElement(type);
type.appendChild(
document.createTextNode(value));
document.getElementById(
"parent").appendChild(type);
}
</script>
</body>
</html>