How to create <input type=“text”/> dynamically

Mohammad Usman picture Mohammad Usman · Apr 14, 2011 · Viewed 189.5k times · Source

I want to create an input type text in my web form dynamically. More specifically, I have a textfield where the user enters the number of desired text fields; I want the text fields to be generated dynamically in the same form.

How do I do that?

Answer

Zach picture Zach · Apr 14, 2011

With JavaScript:

var input = document.createElement("input");
input.type = "text";
input.className = "css-class-name"; // set the CSS class
container.appendChild(input); // put it into the DOM