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 3 of 3
  1. #1
    gin
    gin is offline
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Does javascript have variable variables?

    My form has a slew of text inputs and they are named as below:

    item1 total_item1
    item2 total_item2
    item3 total_item3
    ... (ad infinitum)

    with total_itemX = itemX * 5 (where X is a number)

    I've managed a script that looops through all the elements and grabs the user inputs from the items, but I haven't figured out how to output to the total_items yet. This is what I have so far:

    Code:
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
    	form_field = frm.elements[i] // Get the current field	
    	form_name = form_field.name // Get the field's name	
    	if (form_name.substring(0,4) == "item") { // Is it an "item" field?
    		item_price = 5;		
    		item_quantity = parseInt(form_field.value) // Get the quantity
    
    		var foo = 'total_' + form_name;
    		frm.foo.value = item_quantity * item_price;
    	}	
    }
    The last two lines of script patently don't work. I want to output to an element who's name is in the variable 'foo', not the element called 'foo'. I'd really appreciate if someone could point me in the right direction.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    frm.elements[foo].value = item_quantity * item_price;

    Read more about the bracket notation
    Last edited by glenngv; 06-11-2004 at 10:47 AM.
    Glenn
    ____________________________________

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

  • #3
    gin
    gin is offline
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! I can't believe I missed that >.<


  •  

    Posting Permissions

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