[SOLVED] Problem accessing specific form elements

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 22nd, 2007, 09:58
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
[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.

Code: Select all
<script>
function findTotal() {
var i;

for(i = 0; i <=3; i++)
    {
  
    itemValue = document.step1.elements['foo1'].value;
    alert(itemValue);   
    }
}

</script>

<form name="step1">

<input type="text" name="foo1" onchange="findTotal();" />
<br />
<input type="text" name="foo2" onchange="findTotal();"  />
<br />
<input type="text" name="foo3" onchange="findTotal();"  />
<br />
<input type="text" name="random" onchange="findTotal();"  />
<br />
<input type="text" name="extra" onchange="findTotal();"  />
<br />
Now this is really simple and works fine to provide me with the value of the foo1 field, however I need to find the value of each field named foo1, foo2.... etc.
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

Code: Select all
element = '\'foo' + i'\'';
itemValue = document.step1.elements[element].value;
alert (itemValue);
Code: Select all
itemValue = document.step1.elements['weight'[i]'\''].value;
alert (itemValue);
The error I get with the first attempt is document.step1.elements[element].value has no proporties

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

  #2 (permalink)  
Old Sep 22nd, 2007, 19:04
Reputable Member
Join Date: Dec 2005
Location: U.S.A.
Posts: 147
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to ScottR Send a message via Skype™ to ScottR
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
  #3 (permalink)  
Old Sep 22nd, 2007, 19:39
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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..

Code: Select all
function findTotal() {
// get the form to narrow further getelement calls to just the form
var step1 = document.getElementById('step1');

for(i = 0; i <=3; i++)
    {
     // now loop through the foo's an alert each
  
      alert(step1.getElementById('foo' + i));
    }
}

</script>

<form name="step1" id="step1">

<input type="text" name="foo1" id="foo1" onchange="findTotal();" />
<br />
<input type="text" name="foo2" id="foo2" onchange="findTotal();"  />
<br />
<input type="text" name="foo3" id="foo3" onchange="findTotal();"  />
<br />
<input type="text" name="random" id="random" onchange="findTotal();"  />
<br />
<input type="text" name="extra" id="extra" onchange="findTotal();"  />
<br />
This technique uses the DOM to access nodes of (x)HTML and is supported much more widely.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
  #4 (permalink)  
Old Sep 23rd, 2007, 11:28
Junior Member
Join Date: Apr 2006
Location: UK
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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.
  #5 (permalink)  
Old Sep 23rd, 2007, 11:52
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
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)
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 19:15.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43