Denis and Stefan, thank you so much for all your help. Denis, I don't
know what you're doing in your setup() function. Can you explain or
point me toward a tutorial? Thanks again guys.
Ok, quick explanation:
This html:
<body onload="setup()">
calls the setup function after the document has loaded.
The select list starts out with a single option:
<select id="state" onchange="lookupState();">
<option value="00">Please Choose</option>
</select>
Note that I have given the select element an id property, this makes
referring to it easier.
elId() is shorthand for document.getElementById()
function elId(id) {
return document.getElementById(id);
}
The setup function does two things. It creates the option list for the
html select element based on the properties of the state_array object,
and then it sets the currently selected item to index 0 (first option in
the list):
function setup() {
var v, t, o; // declare variables
for (v in state_array) { // loop through state_array props
t = state_array[v]; // get value of property v
o = document.createElement("option"); // create an option element
o.text = t; // set new option text to t
o.value = v; // set new option value to property name
elId("state").add(o,null); // add new option o at end of select
//element with id "state"
}
elId("state").selectedIndex = 0; // set select element with id
// "state" to first option
}
It could actually be shorter, there's no need to assign t, so the
statement:
t = state_array[v];
can be removed, and the statement
o.text = t;
can be replaced with:
o.text = state_array[v];
This change also means that "t" can be removed from the variable
declaration.
There is also an optimisation that can be made. If you add a declaration
for s to the var statement, and define s before the for loop as:
var s, v, o;
s = elId("state");
for .......
then inside the loop, you can replace:
elId("state").add(o, null);
with:
s.add(o, null);
which saves two function calls for each time through the loop: the call
to elId(), and the wrapped call to document.getElementById(). Not likely
to be a performance issue with 50 odd iterations of the loop, but worth
considering for larger objects.
Rgds
Denis McMahon