View Full Version : Addressing multi word form name?

06-02-2003, 07:44 PM
I have a form that has checkbox with name="foreign pricing" and for various reasons it is not practical to change that name. I want to address the field within a script, like so:
form.canadian pricing[0]
This seems to generate an error. Could I do this:
var can = "canadian pricing";

Or is there some other way to address the field?


06-02-2003, 07:59 PM
you could do this
document.forms["foreign pricing"]

06-02-2003, 08:21 PM
For a form element I think you could try this:
document.forms[0].elements["foreign pricing"]

This would be if the form is the first form on your document. If it is the second form then you would do this:
document.forms[1].elements["foreign pricing"]

Or if you know where the element is the second element in the forms array then you could do this:

Because javascript starts counting at "0" the second element would be "1"

06-02-2003, 08:21 PM
I'd not use spaces at all, but rather underscores or camelCasing.

06-02-2003, 11:18 PM
The document.forms approach worked, thanks!... As far as not using spaces, yes, naturally, that would be the preference - but for various reasons that may not be in the cards.. If function were called so that the form is 'this' (ie. function is validate(orderform) and called by onclick="validate(this)", etc.), it would seem like the following should work?
document.forms.orderform.elements["pricing foregin"] But it does not.

06-03-2003, 02:50 AM
document.forms.orderform.elements["pricing foregin"]
would return something like [document - object ] you need to add something after it
document.forms.orderform.elements["pricing foregin"].value

06-03-2003, 05:46 AM
Thanks, chrismiceli.. Well, this is what I was trying..
if (!document.forms.orderform.elements["pricing foreign"].checked

It tells me that document.forms[orderform] has no properties.

06-03-2003, 06:21 AM
the forms collection allows the use of the lookup operator, which takes the name of the form as a string


this allows you to assert object properties as strings and not as literal properties - which is exactly why it helped you with your space-named form -- because spaces aren't allowed in object/property names because they separate the tokens.

06-03-2003, 09:57 PM
beetle, thanks, that helps.