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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value field inside another value field

Status
Not open for further replies.

frippel

IS-IT--Management
Oct 3, 2002
8
0
0
BE
Is there a way to get this working?

function...

document.form.(document.form.dieselx.value).value= total;
 
Quick lesson:


don't use the notation document.form.blah.value

There are 2 preferred methods instead of this syntax. The first assumes elements have an id attribute assigned:

Code:
<input type="text" [!]id="blah"[/!] value="whatever" />

can be accessed via:

document.[!]getElementById([/!]"blah"[!])[/!].value;

If you do not have id attributes assigned to the elements and are using names instead, you can use the forms and elements collections (this most closely resembles what you are doing above.

Code:
<form [!]id="theForm"[/!]>
   <input type="text" [!]name="blah"[/!] value="whatever" />
</form>

can be accessed via:

document.[!]forms[[/!]"theForm"[!]].elements[[/!]"blah"[!]][/!].value;

Now, with respect to your specific question, since we are using the forms and elements collections - they receive a string to determine which element in the collection you are trying to retrieve.

It's a lot more typing, but who cares? It's a lot less prone to fail, and you won't get any arguments from the firefox error console. I have highlighted in red the derived value used in the outside elements collection:
Code:
document.forms["formName"].elements[[!]document.forms["formName"].elements["dieselx"].value[/!]].value= total;

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[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]
 
Great advice from kaht, and you can, of course, assign things to variables to make the code more compact, e.g.:

Code:
var frm = document.forms["formName"].elements;
frm[frm["dieselx"].value].value = total;

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top