How to make an input in JavaScript

You can change the HTML code of the existing element with innerHTML.

Example:

<div id="myContainer"></div>


<script>
    document.getElementById("myContainer").innerHTML += '<input type="text">';
</script>

Output:

How to add new inputs dynamically?

Sometimes you may want to add inputs dynamically on button click.

It’s also quite simple. All you have to do is to append the HTML code to the existing HTML element.

Example:

<div id="myContainer"></div>

<hr>

<button onclick="addInput()">Add Input</button>

<script>
    function addInput() {
        document.getElementById("myContainer").innerHTML += '<input type="text">';
    }
</script>

Output: