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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2002
    Location
    Mumbai,India
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    valu of a variable made by joining 2 variable names

    Dear All


    I have a typical problem

    I have am accepting qty and value and deriving amount

    There are say 6 such values and I had defined the variable names as follow

    qty0 rate0 amt0
    qty1 rate1 amt1
    qty2 rate2 amt2
    qty3 rate3 amt3
    qty4 rate4 amt4
    qty5 rate5 amt5
    qty6 rate6 amt6


    I want to use for loop to derive amt
    I tried by passing the counter (0,1,2,3,4 ) to a function say calcval

    function calval(cntr) {
    q = 'qty"+cntr+"';
    r='rate"+ntr+"';
    val=q * r;
    }

    Indeed this is not working !


    Thanks in advance for help extended by you .
    I really admire your efforts !

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    function calval(cntr) {
    var val = window['qty'+cntr] * window['rate'+cntr];
    alert(val);
    }

    Learn more info on square bracket notation.

    But I think you can simplify your code by using array instead of multiple variables.
    Code:
    var qty = new Array(1,2,3);
    var rate = new Array(1.5, 2.0, 4.5);
    var amt = new Array(10.00, 4.50, 5.75);
    
    function calval(cntr) {
      var val = qty[cntr] * rate[cntr];
      alert(val);
    }
    Remember that arrays are zero-based.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Oct 2002
    Location
    Mumbai,India
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My problem :

    I had defined

    var d="document.frm";
    var q=".qty";
    var v= ".value";
    var qv = d+q+cntr+v;

    if I say

    alert(qv)

    e value is displayed as

    document.frm.qty0.value

    This means a variable whose value I need is formed !

    But how do I get value stored in the same

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    alert( document.frm.elements["qty"+cntr].value );

    Follow the square bracket notation link that I posted earlier. It will greatly help you in javascript programming.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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