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?
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?
You could do something like this in a loop based on the number of text fields they enter.
But for better performance I'd create all the html first and inject it into the DOM only once.
Edit this example uses jQuery to append the html, but you could easily modify it to use innerHTML as well.
Maybe the method
document.createElement();
is what you're looking for.See this answer for a non JQuery solution. Just helped me out!
Adding input elements dynamically to form
With JavaScript:
Using Javascript, all you need is
document.createElement
andsetAttribute
.Then you can use
appendChild
to append the created element to the desired parent element.I think the following link will help you. If you want to generate fields dynamically and also want to remove them on the same time you can get the help from here. I had the same question, So i got the answer
http://jsfiddle.net/jaredwilli/tZPg4/4/