Greetings to all, The idea here is to have a function in javascript that would take the value from one input box (lets call it txbA) and use it as the limit for a while loop or for-next loop and create a series of input boxes each with a unique name so that its value (when entered or changed)could later be used in other calculations.
When I try something like:
function MakeMany()
{
var y=parseInt(txbA.value);
for(var x=0;x<y;x++)
{
document.write("<input type='text' name= ???? /><br />");
} // end for loop
} // end function
You can call this from a button and get the text boxes, but how do I get them to have unique names such as MMtb1, MMtb2, MMtb3 etc. ??
I'm thinking that there must be a way, but everything I've tried so far has gotten me no where!
Stephen
creating textboxes in loopcreating new input (text) boxes with function
Page 1 of 1
2 Replies - 3642 Views - Last Post: 22 December 2009 - 10:43 PM
Replies To: creating textboxes in loop
#2
Re: creating textboxes in loop
Posted 22 December 2009 - 10:42 PM
In your for loop, use a variable for getting unique names:
var uniqueNames= "MMtb"+x;
document.write("<input type='text' name= uniqueNames /><br />");
#3
Re: creating textboxes in loop
Posted 22 December 2009 - 10:43 PM
here you are using document.write and it is clearing everything from the page and creating the text boxes,
so I suggest to you to create a div and create text boxes in div,
and you can name your textbox something like this txtNum[], and when you are submitting it then you can get these as an array of textbox.
Here is the code
or you can name it something like this str += "<input type='text' id='txtNum"+ i +" /> <br/>";
Hope it helps
so I suggest to you to create a div and create text boxes in div,
and you can name your textbox something like this txtNum[], and when you are submitting it then you can get these as an array of textbox.
Here is the code
<script type="text/javascript">
function createMany(nums){
var str = "";
for(i=0;i<nums;i++){
str += "<input type='text' id='txtNum[]' /> <br/>";
}
document.getElementById("divTxt").innerHTML = str;
}
</script>
<p>
<input type="text" name="txtNums" id="txtNums" />
<input type="button" name="button" id="button" value="Create" onclick="createMany(txtNums.value);" />
</p>
<div id="divTxt"></div>
or you can name it something like this str += "<input type='text' id='txtNum"+ i +" /> <br/>";
Hope it helps
This post has been edited by noorahmad: 22 December 2009 - 10:46 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|