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 13 of 13
  1. #1
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    variable variables in javascript

    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:
    PHP Code:
    for ($i=$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?
    c:\dos...
    c:\dos\run...
    run dos run

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Without seeing your form I would guess something like this:

    Code:
    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

  • #3
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    c:\dos...
    c:\dos\run...
    run dos run

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    again thanks KOR but what does the forms[0] stand for....
    this is probably a really really stupid question.
    c:\dos...
    c:\dos\run...
    run dos run

  • #6
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    c:\dos...
    c:\dos\run...
    run dos run

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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....
    Cheers;
    .....Willy

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "Variable variables" are pretty much arrays...

    Read this:
    http://webmonkey.wired.com/webmonkey...tw=programming

    And the next page too, on Arrays & Loops.

  • #12
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cool,
    i got it sorted
    thanks a lot guys
    c:\dos...
    c:\dos\run...
    run dos run

  • #13
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    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).....
    Last edited by Willy Duitt; 08-20-2004 at 02:33 PM.


  •  

    Posting Permissions

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