This is a discussion on "[SOLVED] Problem accessing specific form elements" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Problem accessing specific form elements are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
[SOLVED] Problem accessing specific form elements
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
[SOLVED] Problem accessing specific form elements
Hi all,
I'm a complete javascript noob and have a problem that has been bugging me for a couple of days now. I have something similar to the following.
I have tried adding the following code to the function so it can access the element name concatenated with the value of i in the loop
The error associated with the second method is findTotal is not defined. I know I will be kicking myself when somebody tells me the correct syntax, but this is really bugging me! TIA |
|
|
|
|||
|
Re: Problem accessing specific form elements
I would usually use getElementById in this instance after I gave the elements ids. What I've seen about the method you are using is that element is an array of form elements. So it would seem logical to me that they would be integers unless you created variables naming those array elements.
Although I haven't used elements[] so I can't be much help. Scott |
|
||||
|
Re: Problem accessing specific form elements
There's a couple of problems with the code you've give.
element = '\'foo' + i'\''; is storing a string with the apostrophe literals in there so it is equivalent to using document.etc.elements['\'foo'+i+'\'']; which is obviously wrong. As ScottR says above though it is much better to use getElementById(); If you give each form element an ID you can acheive this. Accessing elements by name only is something that really only works reliably in IE. If you give all you elements an ID to match its name you can rewrite your function like so..
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
|||
|
Re: Problem accessing specific form elements
Thanks very much for your help guys, the getElementById(); function was exactly what I needed - now completed my function and it works a treat.
|
|
||||
|
Re: Problem accessing specific form elements
You don't *have* to use getElementdById, it will work the same either way. The problem is that you were looping through numbers 1 to 3 but you were looking for the same element every time.
All you have to do is change this... itemValue = document.step1.elements['foo1'].value; ...into this: itemValue = document.step1.elements['foo'+i].value;
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [SOLVED] accessing table data | alexhkleung | JavaScript Forum | 4 | Dec 2nd, 2007 04:55 |
| Drop down form elements | 1840dsgn | Web Page Design | 3 | Sep 4th, 2007 19:09 |
| Simple form elements question! | jtotheb | PHP Forum | 3 | Jul 18th, 2007 14:54 |
| problem accessing page | number9 | Starting Out | 4 | May 25th, 2007 05:30 |
| Disabling/Enabling form elements | flamin_mongrel | JavaScript Forum | 1 | Oct 18th, 2006 21:51 |