gorilla1
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";
form.can[0]
Or is there some other way to address the field?
G
chrismiceli
06-02-2003, 07:59 PM
you could do this
document.forms["foreign pricing"]
requestcode
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:
document.forms[0].elements[1]
Because javascript starts counting at "0" the second element would be "1"
beetle
06-02-2003, 08:21 PM
I'd not use spaces at all, but rather underscores or camelCasing.
gorilla1
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.
G
chrismiceli
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
gorilla1
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.
beetle
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
document.forms['orderform']
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.
gorilla1
06-03-2003, 09:57 PM
beetle, thanks, that helps.
G