Get the number of text fields from the user and assign it to a variable.
var no = document.getElementById("idname").value
To create input fields, use createElement
method and specify element name i.e. "input" as parameter like below and assign it to a variable.
var textfield = document.createElement("input");
Then assign necessary attributes to the variable.
textfield.type = "text";
textfield.value = "";
At last append variable to the form element using appendChild
method. so that the input element will be created in the form element itself.
document.getElementById('form').appendChild(textfield);
Loop the 2,3 and 4 step to create desired number of input elements given by the user inside the form element.
for(var i=0;i<no;i++) {
var textfield = document.createElement("input");
textfield.type = "text"; textfield.value = "";
document.getElementById('form').appendChild(textfield);
}
Here's the complete code
function fun() {
/*Getting the number of text fields*/
var no = document.getElementById("idname").value;
/*Generating text fields dynamically in the same form itself*/
for(var i=0;i<no;i++) {
var textfield = document.createElement("input");
textfield.type = "text";
textfield.value = "";
document.getElementById('form').appendChild(textfield);
}
}
_x000D_
<form id="form">
<input type="type" id="idname" oninput="fun()" value="">
</form>
_x000D_