Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Addressing multi word form name?

    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

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could do this
    document.forms["foreign pricing"]

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd not use spaces at all, but rather underscores or camelCasing.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by gorilla1; 06-03-2003 at 01:10 AM.

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    beetle, thanks, that helps.
    G


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •