#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    108
    Rep Power
    12

    Javascript syntax question.


    Hi guys,
    i'm trying to run this function:

    PHP Code:
    function memberNameFocus(formName)
    // Focuses the cursor on the member username field.
    {
        
    document.formName.memberName.focus();

    But no matter what parameter I enter into the function for formName is still remains formName.
    How do i make the function grab the variable?
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    Code:
    document[formName].memberName.focus();
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    108
    Rep Power
    12
    Originally posted by Clone53421
    Code:
    document[formName].memberName.focus();
    in this case, puttng [ ] around the parameter in the function, why wouldn't it be

    document.[formName]. ...
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    You are not referencing it as a function, which would be given arguments
    with (), but rather as an array. To reference array elements you use brackets.

    As far as I know, any object structure can be dually referenced as an array
    structure, and vice versa. Thus...
    document.form == document["form"]
    MyArray["Name"] == MyArray.Name

    This can come in handy for situations such as the following. Run this and see
    what I mean. The variable p will contain a text string naming one of the
    properties of the document object.
    Code:
    <html>
    <body>
    Properties of the document object:<br><br>
    <script>
    for (var p in document) {
      document.write("<b>document." + p + " == document[\"" + p + "\"] == </b> " + document[p] + "<br>");
    };
    </script>
    </body>
    </html>
    For this reason, like any other array, you need no period between the
    object name and the brackets.
    Last edited by Clone53421; August 9th, 2003 at 11:29 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    108
    Rep Power
    12
    Thanks!
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    Glad to be able to help...

IMN logo majestic logo threadwatch logo seochat tools logo