...

View Full Version : variable variables in javascript



earthsiege
08-20-2004, 02:02 PM
hello guys,
i was just writing up a simple script which takes some values from a number of different fields which are names similiarly like price1, price2, price3 etc and adds them up to make a total figure.
Then i though to myself that woould it ne easier to use a loop to make the amount of coding a helluva lot smaller.
but the problem is i cant figure out how to use variable variables in javasript.
for example in php id use something like:

for ($i=0 ; $i < 5; $i++){
$total = $total + ${"price".$i};
}
print ("$total");

is there anyway way that a similiar thing can be done with javascript where the valuse are read from fields with similiar names and the out put is given to another field?

Willy Duitt
08-20-2004, 02:30 PM
Without seeing your form I would guess something like this:



var total = 0;
for (var i=0; i < 5; i++){
total = total*1 + document.forms[0]['price'+i].value*1;
} document.forms[0]['total'].value = total;



.....Willy

earthsiege
08-20-2004, 02:39 PM
thanks willy but can i ask what the the forms[0] is for? imean what does the the 0 stand for?
and how would i substitute my form name in there?

Kor
08-20-2004, 02:39 PM
Probably the easiest way is to set the number of text fields whose values are to be add.

Presuming that you have names price1, price2.... price7

n=7// number of text fields values you want to add
var total='';//initialize the variable total
for(var i=1;i<n;i++){
total=total+parseInt(document.forms[0].elements['price'+i].value);
}
alert(total)

Keep in mind that the input's values are strings, so you must parse them into integer. If you have/need floating values, use parseFloat() method.

There might be some other methods to make it full dynamic (using getElementsByTagName or some other DOM methods, but they might be a little bit more intricate)

earthsiege
08-20-2004, 02:41 PM
again thanks KOR but what does the forms[0] stand for....
this is probably a really really stupid question.

earthsiege
08-20-2004, 02:48 PM
this is gonna sound really funny but whenever i make any changes to the fields the total i get for my total is 1200170022003000.
help anyone?

Kor
08-20-2004, 02:52 PM
forms and form's elements are to be refered also by their index order, as they are arrays too.

Thus form[0] is the first form of the page (from top to bottom). Most of the time pages use a single form, so you don't need to name them.

the full form's reference syntax is

document.forms[name_or_index].elements[name_or_index]

if name, quote it as it is a string.

Willy Duitt
08-20-2004, 02:53 PM
The 0 refers to the first form on the page....
I used that reference as I did not know what the name of your form was and this is a generally accepted generic way to provide example code....

If there is only the one form on the page, you can leave that as is, If there is more than one form you can either replace the 0 with the number which corresponds to the particular forms place in the forms collection or replace the 0 with the forms name wrapped in quotes....

And Kor;

FWIW; I see nothing wrong with using the shortcut of multiplying by one to convert strings to numbers....

Although most people use parseFloat(), parseInt() and number().... I prefer *1......

To each his own tho.... :D
Cheers;
.....Willy

Kor
08-20-2004, 02:54 PM
this is gonna sound really funny but whenever i make any changes to the fields the total i get for my total is 1200170022003000.
help anyone?


parse the values (see my previous replay)

Kor
08-20-2004, 02:58 PM
Although most people use parseFloat(), parseInt() and number().... I prefer *1......

yes, I know, it's up to anyone... I am used with parse... Yes, probably *1 is a better ideea as you don't bother with integer or float problems...

by the way, I don't understand why the Number() syntax is Upper cased...

AaronW
08-20-2004, 03:04 PM
"Variable variables" are pretty much arrays...

Read this:
http://webmonkey.wired.com/webmonkey/98/04/index1a_page8.html?tw=programming

And the next page too, on Arrays & Loops.

earthsiege
08-20-2004, 03:12 PM
cool,
i got it sorted
thanks a lot guys

Willy Duitt
08-20-2004, 03:16 PM
by the way, I don't understand why the Number() syntax is Upper cased...

Hmmm;

Interesting observation.....
I don't know.... I hardly if ever use Number() and never ever considered this deviation from the norm of camelCasing...


Additionally, I hardly use parseFloat either because I almost exclusivly format the number in some manner..... Whether it be to a certain amount of decimal places and/or currency/commas....

.....Willy

Edit: A quick check shows that the UpperCase N in Number is because it is an object.constructor and follows the syntax as such... (new Array, new Image, new Object).....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum