2

I am trying to iterate thru arrays to get country info for a dropdown.

These are the arrays:

var array_states_US = new Array("AL", "AK", "AR")
var array_states_names_US = new Array("Alabama", "Alaska", "Arkansas")

var array_states_MX = new Array("AG", "BN", "BS")
var array_states_names_MX = new Array("Aguascalientes", "Baja California", "Baja California Sur")

Here is the function:

function buildDropdown(countryISO){
    var tmpArry = 'array_states_'  + countryISO;
    var tmpArryNames = 'array_states_names_' + countryISO;
    $(tmpArry).each(function(i) {
                $(stateSelectId).append($("<option></option>").attr("value", this).text($(tmpArryNames)[i]));
    });
}

But JQuery sees $(tmpArry) and $(tmpArryNames) as strings instead of the array values. (When I used MooTools in a past project for this, the dynamic var was not a problem.) If i use $(array_states_MX) and $(array_states_names_MX) instead, then it works. But the countryISO value has to be dynamic.

What is the correct syntax to make this work?

0

3 Answers 3

4

you can use the window object as all global vars are stored there.

tmpArry = 'array_states_'+countryISO;
$(window[tmpArry]).each(){}

alternatively, why not use nested arrays instead of naming them separate things?

array_states[countryISO]
Sign up to request clarification or add additional context in comments.

Comments

0

To solve the problem as you have it now, you need to use eval() on your tmpArray and tmpArrayNames variables:

var tmpArray = eval('array_states_' + countryISO);

However eval is a dangerous function that you should really avoid, by restructuring your code.

4 Comments

I would love to get rid of eval(), which is what i did, but apparently it broke it. So I haven't the faintest idea of how to do it differently.
Now that this is working, it turns out IE doesn't like the switch-case statement. It sees countryISO=MX, but it doesn't find it in the list of cases: switch (countryISO) { case "US": case "CA": case "AT": case "MX": case "IE": case "AU": case "MY": case "AR": alert('country ' + countryISO + ' found');break; default: alert('country ' + countryISO + ' not found'); break;
@Dawn - It's difficult to help when the formatting is all wrong in your comment, but if that's exactly what your code is, you are missing the closing curly brace of the switch statement, and countryISO=MX should probably be countryISO="MX" (notice the quotes), unless MX is a variable defined elsewhere.
yes, when i changed the value of countryISO to countryISO.toString(), then it worked in IE. It's just so weird how i didn't have to do that extra step when working with MooTools. JQuery definitely has its benefits, but not when you're going through all your existing MooTools code and translating it into JQuery. :)
0

I personally would restructure this completely to use objects..

var isoStates = {
    'US': {
        'AL': 'Alabama',
        'AK': 'Alaska',
        'AR': 'Arkansas'
    },
    'MX': {
        'AG': 'Aguascalientes',
        'BN': 'Baja California',
        'BS': 'Baja California Sur'
    }
};

function buildDropdown(countryISO){
    var states = isoStates[countryISO];
    for(state in states)
    {
        $(stateSelectId).append($("<option></option>").attr("value", state).text(states[state]));
    }
}

working demo at http://jsfiddle.net/gaby/6gbWJ/

2 Comments

This sounds good, but the original code also includes additional vars: text_zip_label_MX = "Postal Code:" and text_label_MX = "State:" for each isoState. So either those would also need to be in the array, or handled as a separate array check. I'm thinking the latter. This whole piece of code clearly needs rewriting...it's legacy code.
@Dawn, yes you could have a labels object that works the same way ... var isoLabels = {'MX':{ 'text_zip':'Postal Code', 'text':'State'}, 'US':{...}} But if there is already working code and you want to just minimize changes, then it might be best to just use the window trick proposed in @contagious answer..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.