Concatenating two parts to define a variable name inside a loop

Discussion in 'JavaScript' started by jonnyewing, May 10, 2012.

  1. #1
    Question from a Javascript novice, so please be gentle with me.

    I've had trouble finding a solution to this on the web because I don't really know the right words to search for.

    Here's a bit of script I am working on that works fine...

    
    if (booking_form.children.selectedIndex == 2)
    {
    	if (booking_form.age_of_child_2.value == undefined)
    	{
    		alert("Please enter the age of your child.");
    		return false;
    	}
    }
    
    Code (markup):
    So far, so good.

    However, I need to put it into a loop so that where I have got a "2" above, I can replace the 2 with an "i" where "i" will be equal to booking_form.age_of_child_1, booking_form.age_of_child_2, booking_form.age_of_child_3 etc

    Can someone explain how that's done?
     
    Solved! View solution.
    jonnyewing, May 10, 2012 IP
  2. OMGCarlos

    OMGCarlos Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok, I think I understand what you want. What you want to do is replace:

    if (booking_form.age_of_child_2.value == undefined)
    Code (markup):
    with something like:

    if ("booking_form.age_of_child_" + i + ".value" == undefined)
    Code (markup):
    right? If so, then yes you can! There are actually two ways, the first would require that those variables be global (not defined with var, and inside the same scope). You then simply wrap the variable string with window[].

    The window[] array stores a list of all the global stuff, ie variables, functions, etc. , which can be looked up with by it's string representation. In other words, if you have a global variable called test, you can look it up as window["test"], or window["te"+"st"] or whatever. To be honest, I couldn't google it either, so I whipped up this Fiddle for you, (I just joined, so I can't enter links, just copy/paste it)

    http://jsfiddle.net/QPzKA/

    The other way is by using eval(), but eval can be...risky.
     
    OMGCarlos, May 10, 2012 IP
  3. #3
    if (booking_form.age_of_child_2.value == undefined)
    
    PHP:
    is equal to this:


    if (booking_form['age_of_child_2'].value == undefined)
    
    PHP:
    and now you can easily replace:

    var i = 2;
    if (booking_form['age_of_child_' + i].value == undefined)
    
    PHP:
    http://www.quirksmode.org/js/associative.html - Read "Associative arrays" section.
     
    e-abi, May 10, 2012 IP
  4. jonnyewing

    jonnyewing Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    Thanks, OMGCarlos - will give this a try.
     
    jonnyewing, May 11, 2012 IP
  5. jonnyewing

    jonnyewing Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Thanks for the tip and link, e-abi
     
    jonnyewing, May 11, 2012 IP