Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading values from dynamically created text boxes... 1

Status
Not open for further replies.

Chewie71

MIS
Sep 4, 2001
89
US
I've been implementing some AJAX/Javascript and have been playing around with some dynamic forms. I'm using AJAX so that when a user enters data into a textbox, AJAX validates that data and then creates a new textbox so they can enter more data to be validated. I'm trying to figure out how to get javascript to read back the values from an unknown number of these textboxes. There may be one...there may be five...

The textbox code looks something like this...
Code:
<input type=text name='cd0' onblur='populateSections(document.forms[\"adminForm\"], this.value, \"user\");'>
<input type=text name='cd1' onblur='populateSections(document.forms[\"adminForm\"], this.value, \"user\");'>
<input type=text name='cdn' onblur='populateSections(document.forms[\"adminForm\"], this.value, \"user\");'>

In my Javascript function I want to be able to read in an 'n' number of values from textboxes named 'cd1' through 'cdn'. Something like this...

Code:
x=0;
while (document.myFormName.cdx)
{
  readData = document.myFormName.cdx.value;
  //Do some AJAX stuff with the readData (rebuild the textboxes and add a new empty textbox with name="cdx+1"
  x++;  //Go around again and read the next textbox value if there is one
}

I know this doesn't work, and I can't seem to find anyplace that says how to make this work. Is anyone else doing something like this?

Thanks,
Matt
 
you need to use the forms and elements collections (and should get used to using them always, you'll find you get a lot less problems when you use them consistently)
Code:
var x = 0;
var readData;
while (document.[!]forms[/!]["formName"].[!]elements[/!]["cd" + x]) {
   readData = document.[!]forms[/!]["formName"].[!]elements[/!]["cd" + x].value;
   //do stuff
   x++;
}

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top