SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript question

    I have a form input element that has a name like "First Name"
    I want to reference this in javascript, but when I do something like document.formname.First Name.value, I get an error.

    It doesn't like the space in the name. Assuming I cannot change the name of the form element, how can I reference it in javascript?
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.formname.elements["First Name"].value might work, otherwise use an underscore, because you should never have spaces in variable names (standard programming practice).

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also reference it based on its position in the form. For instance if it is the first element in your form you can do this:
    var myvar=document.form_name.elements[0].value

    If you have multiple forms you can also reference the form that way:
    var myvar=document.forms[0].elements[0].value

    This would populate myvar with the value in the first element in the first form.

    You can also determine what type it is like this:
    if(document.forms[0].elements[0].type="text")
    {
    Then do something
    }

    You should also be able to determine the name:
    if(document.forms[0].elements[0].name="some name")
    {
    Then do something
    }

    Of course you should use a for loop to bounce through each form element to check these types of things. Hope this helps and good luck.

  4. #4
    grasshoppa Snowbird122's Avatar
    Join Date
    Apr 2001
    Location
    Austin
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you everyone for your help.

    The form elements were radio buttons, so there was no way to reference them by index; I had to use their name. I "solved" the problem by replacing the spaces with underscores.

    I appreciate your help.
    http://www.echo-consulting.net - Sound Solutions for Online Inspriations.


Bookmarks

Posting Permissions

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