View Full Version : valu of a variable made by joining 2 variable names

07-14-2004, 04:43 AM
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+"';
val=q * r;

Indeed this is not working !

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

07-14-2004, 05:26 AM
function calval(cntr) {
var val = window['qty'+cntr] * window['rate'+cntr];

Learn more info on square bracket notation (http://www.litotes.demon.co.uk/js_info/sq_brks2.html).

But I think you can simplify your code by using array instead of multiple variables.

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];

Remember that arrays are zero-based.

07-14-2004, 07:56 AM
My problem :

I had defined

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

if I say


e value is displayed as


This means a variable whose value I need is formed !

But how do I get value stored in the same

07-14-2004, 08:03 AM
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.