SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Referencing a Variable of a Variable?

    I'm not sure if my subject title makes any sense, but can I do the following in JS:

    Let's say I have three text fields on a form: field1, field2, and field3. If I were to loop through the three fields, how I could reference them using their NAME attributes? That is, instead of:

    Code:
    for (var i = 0; i < 3; i++) {
        alert(document.someForm.elements[i].value);
    }
    I would like to say

    Code:
    for (var i = 0; i < 3; i++) {
        var fieldName = 'field' + i;
        alert(document.someForm.fieldName.value);
    }
    Similarly, if I had three variables: var1, var2, and var3. Can I say:

    Code:
    for (var i = 0; i < 3; i++) {
        var varName = 'var' + (i + 1);
        varName += 10;
    }
    Thanks,

    Sean

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in javascript

    someObject.someProperty === someObject ["some" + "Property"]

    someGlobalVar === window["some" + "Global" + "Var"]

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the proper way to identify form-elements:

    Code:
    document.forms["form-name"].elements["element-name"].XYZ

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, kaimeder.

    Is it acceptable to substitute the form name and input name?

    So, instead of:

    document.forms[0].elements[1].value

    I can say:

    document.someForm.someField.value?

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ::: certified wild guess :::

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perfectly acceptable but, in my opinion, kind of dirty.

    with square-brackets you are even able to reference element-names with names like "foobar[]" (which would be an array if processed by PHP)

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    adios, thanks for the link. I didn't realize you could specify the NAME attributes using bracket notation, which is why I've always the "shortcut" notation.

    kaimeder, I'll definitely "clean up" my form code. Thanks for alerting me to this "best practice" of Javascript. I just wish I could find similar examples, as I'm learning Javascript rather informally (just by researching the Web).

    Thanks again,


    Sean


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
  •