GalanopD GalanopD - 2 months ago 7
Javascript Question

I can't make it "see", Antigua and Bermuda, i think because of the spaces?

function ff_CselRegion_action(element, action)
{
country = ff_getElementByName('CselCountry');
state = ff_getElementByName('CselState');

var America_NorthArray = new Array('Antigua and Barbuda', 'Canada', 'Greenland', 'USA', 'Mexico');
if (element.value == 'none') {
// drop countries
while (country.options.length > 0)
country.options[country.options.length-1] = null;
} else {
// get the matching array
var selectedArray = eval(element.value+'Array');

// drop supernumerous option
while (selectedArray.length < country.options.length-1)
country.options[country.options.length-1] = null;

// add the new options
country.options[0] = new Option('Select Country', 'none', true, true);
for (var i = 0; i < selectedArray.length; i++)
country.options[i+1] =
new Option(selectedArray[i], selectedArray[i]);
} // if

// drop states
while (state.options.length > 0)
state.options[state.options.length-1] = null;
} // ff_CselRegion_action
function ff_CselCountry_action(element, action)
{
state = ff_getElementByName('CselState');

var stateArrayAntigua and Barbuda = new Array(
'Saint George', 'Saint John', 'Saint Mary', 'Saint Paul'

var selectedArray = eval('stateArray'+element.value);


if (selectedArray == null) {
while (state.options.length > 0)
state.options[(state.options.length - 1)] = null;
} else {
// drop supernumerous option
while (selectedArray.length < state.options.length-1)
state.options[state.options.length-1] = null;

// add the new options
state.options[0] = new Option('Select State', 'none', true, true);
for (var i=0; i < selectedArray.length; i++)
state.options[i+1] = new Option(selectedArray[i], selectedArray[i]);
} // if
} //ff_CselCountry_action


I am trying to find a solution for (
var stateArrayAntigua and Barbuda
) because it has spaces and cannot "see" the 'Antigua and Barbuda' at the (
var America_NorthArray
). As I am new in programming, I would appreciate your answers to be as simple as possible. Thank you in advance

P.S. I searched on the internet but I haven't managed to work it out.

Answer

You can't have spaces inside variables' names. Make it var stateArrayAntigua_and_Barbuda and you're good to go (if there's no other problems with your code).

If you'd like to to learn this more in depth, you may want to read this but basically you'd better use latin letters digits, _ and $ in variables' names.

PS in the lines

var stateArrayAntigua and Barbuda = new Array(
    'Saint George', 'Saint John', 'Saint Mary', 'Saint Paul'

you also got a syntax error: the closing bracket is missing.