In this tutorial, you will learn how to generate a textbox on button click using javascript. Here is a javascript example that accepts the number from the user in order to generates the specific number of textboxes. On clicking the button with respect to each textbox, it will give you the value of that particular textbox.
Example
<html>
<script>
function generate()
{
var no = document.getElementById("text").value;
var tbl = document.getElementById("div");
for(var i =1;i<=no;i++)
{
tbl.innerHTML = tbl.innerHTML +' <input type="button" value="ok" onclick="getData('+i+');">'+'<input type="text" id=text'+i+'><br>\n';
}
}
function getData(i){
var value=document.getElementById('text'+i).value;
alert(value);
}
</script>
<body>
<pre>
<input type="button" value="OK" onclick="generate()"/><input type="text" id="text" />
</pre>
<div id="div">
</div>
</body>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.