creating textboxes in loop

creating new input (text) boxes with function

Page 1 of 1

2 Replies - 13170 Views - Last Post: 22 December 2009 - 10:43 PM

#1 sblund  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 30-October 08

creating textboxes in loop

Post icon  Posted 22 December 2009 - 10:30 PM

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!


Is This A Good Question/Topic? 0
  • +

Replies To: creating textboxes in loop

#2 chirag.jain18  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 68
  • Joined: 14-December 09

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 />");

Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

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
<script type="text/javascript">
function createMany(nums){
	var str = "";
		str += "<input type='text' id='txtNum[]' /> <br/>";
	document.getElementById("divTxt").innerHTML = str;
  <input type="text" name="txtNums" id="txtNums" />
  <input type="button" name="button" id="button" value="Create" onclick="createMany(txtNums.value);" />

<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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1