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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Simple Syntax Question

    I am going to have a lot of code like this:

    // Function to set item
    function setItem(n){
    if (n==1){V=parent.image.document.formA.pro1.value}
    if (n==2){V=parent.image.document.formA.pro2.value}
    if (n==3){V=parent.image.document.formA.pro3.value}
    if (n==4)...
    }

    So I am wondering if I can shorten it by simply having the variable "n" which is handed to the function replace the number digit in "pro"

    Something like this:

    // Function to set item
    function setItem(n){
    V=parent.image.document.formA.pro[n].value;
    }

    I'm sure the answer's really easy or it's not possible. Thanks in advance!

    Peter

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts

    Re: Simple Syntax Question

    Code:
    // Function to set item
    function setItem(n){
      V=parent.image.document.forms["formA"].elements["pro" + n].value;
    }
    should do the trick
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Works great... thank you.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Follow-up question. (I get at least one, don't I?)

    In the form the values are pulled from variables with default values, then the user can change the values in the form. The new values are then used by a function and Roelf's code works great.

    But I also want to put the values the user entered into the variables to replace the defaults. So now my "simple syntax" question is how do I address a variable, e.g., pro1, pro2, pro3... replacing the numeric digit with "n" as handed to the function.

    Something like:

    function setVariable(n){
    pro[n]=whatever

    Thanks again!

    Peter

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    why create different variables, and not an array like:
    var pro = new Array();
    pro[n] = whatever;

    then you have all your values together
    I am the luckiest man in the world

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Great idea. I actually only have 3 now and it's working coded the long way, but I'll do that when I expand.

    As to why not... I'm learning!

    Thanks again, Peter


  •  

    Posting Permissions

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