Choopernickel
11-07-2003, 10:01 PM
Gents, Ladies,
I'm having some problems with a homegrown related selects function. In this case, we're looking at counties and cities, and I can't seem to find the problem -- specially since it works in Firebird.
IE doesn't load the options on pageload, and onchange of countySelect, it will alert this:
countySelect.options[countySelect.selectedIndex].text
as nothing. Not null, just nothing.
The error reported is
Line 31
Char 12
'citiesByCounty[...].length' is null or not an object
Here's the code; any pointers will be helpful.
<!-- ... -->
<script type="text/javascript">
var counties = new Array();
var citiesByCounty = new Object();
var countySelect, citySelect;
function countiesInit () {
countySelect = document.getElementById('_County');
citySelect = document.getElementById('_City');
for (i=0;i<counties.length;i++) {
cntyOpt = document.createElement("option");
cntyOpt.text = counties[i];
cntyOpt.value = counties[i];
countySelect.appendChild(cntyOpt);
}
countySelect.onchange = populateCities;
}
function populateCities () {
citySelect.innerHTML = "";
if (countySelect.selectedIndex < 1) {
return;
}
var chosenCounty = countySelect.options[countySelect.selectedIndex].text;
var currentCity;
for (i=0;i<citiesByCounty[chosenCounty].length;i++) {
cityOpt = new Option(citiesByCounty[chosenCounty][i]);
citySelect.appendChild(cityOpt);
if (typeof(countySelect.defaultValue) != 'undefined' &&
countySelect.defaultValue == chosenCounty &&
typeof(citySelect.defaultValue) != 'undefined' &&
citySelect.defaultValue == currentCity) {
citySelect.selectedIndex = i;
}
}
}
counties[counties.length]='Appling';
citiesByCounty['Appling'] = new Array();
tmp = citiesByCounty['Appling'];
tmp[tmp.length]='Baxley';
tmp[tmp.length]='Graham';
tmp[tmp.length]='Holmesville';
tmp[tmp.length]='Surrency';
counties[counties.length]='Atkinson';
citiesByCounty['Atkinson'] = new Array();
tmp = citiesByCounty['Atkinson'];
tmp[tmp.length]='Pearson';
tmp[tmp.length]='Willacoochee';
counties[counties.length]='Bacon';
citiesByCounty['Bacon'] = new Array();
tmp = citiesByCounty['Bacon'];
tmp[tmp.length]='Alma';
// et cetera, et cetera
function pageInit() {
countiesInit();
// loop over countySelect options/
for (var i=countySelect.options.length;i--;i>=0) {
// compare to #query_edit_HevacCounter.county#
if (countySelect.options[i].text == 'Fulton') {
// on match, set selectedIndex and defaultValue of countySelect
countySelect.selectedIndex = i;
countySelect.defaultValue = 'Fulton';
}
}
countySelect.onchange();
// loop over citySelect options
for (var j=citySelect.options.length;j--;j>=0) {
//compare to #query_edit_HevacCounter.city#
if (citySelect.options[j].text == 'Alpharetta') {
// on match, set selectedIndex and defaultValue of citySelect
citySelect.selectedIndex = j;
citySelect.defaultValue = 'Alpharetta';
}
}
}
window.onload = pageInit;
</script>
</head>
<body>
<form>
<!-- ... -->
<label for="_County">County: (#county#)</label>
<select name="_County" id="_County"></select>
<label for="_City">City: (#city#)</label>
<select name="_City" id="_City"></select>
<!-- ... -->
</form>
</body>
TIA,
Choop.
I'm having some problems with a homegrown related selects function. In this case, we're looking at counties and cities, and I can't seem to find the problem -- specially since it works in Firebird.
IE doesn't load the options on pageload, and onchange of countySelect, it will alert this:
countySelect.options[countySelect.selectedIndex].text
as nothing. Not null, just nothing.
The error reported is
Line 31
Char 12
'citiesByCounty[...].length' is null or not an object
Here's the code; any pointers will be helpful.
<!-- ... -->
<script type="text/javascript">
var counties = new Array();
var citiesByCounty = new Object();
var countySelect, citySelect;
function countiesInit () {
countySelect = document.getElementById('_County');
citySelect = document.getElementById('_City');
for (i=0;i<counties.length;i++) {
cntyOpt = document.createElement("option");
cntyOpt.text = counties[i];
cntyOpt.value = counties[i];
countySelect.appendChild(cntyOpt);
}
countySelect.onchange = populateCities;
}
function populateCities () {
citySelect.innerHTML = "";
if (countySelect.selectedIndex < 1) {
return;
}
var chosenCounty = countySelect.options[countySelect.selectedIndex].text;
var currentCity;
for (i=0;i<citiesByCounty[chosenCounty].length;i++) {
cityOpt = new Option(citiesByCounty[chosenCounty][i]);
citySelect.appendChild(cityOpt);
if (typeof(countySelect.defaultValue) != 'undefined' &&
countySelect.defaultValue == chosenCounty &&
typeof(citySelect.defaultValue) != 'undefined' &&
citySelect.defaultValue == currentCity) {
citySelect.selectedIndex = i;
}
}
}
counties[counties.length]='Appling';
citiesByCounty['Appling'] = new Array();
tmp = citiesByCounty['Appling'];
tmp[tmp.length]='Baxley';
tmp[tmp.length]='Graham';
tmp[tmp.length]='Holmesville';
tmp[tmp.length]='Surrency';
counties[counties.length]='Atkinson';
citiesByCounty['Atkinson'] = new Array();
tmp = citiesByCounty['Atkinson'];
tmp[tmp.length]='Pearson';
tmp[tmp.length]='Willacoochee';
counties[counties.length]='Bacon';
citiesByCounty['Bacon'] = new Array();
tmp = citiesByCounty['Bacon'];
tmp[tmp.length]='Alma';
// et cetera, et cetera
function pageInit() {
countiesInit();
// loop over countySelect options/
for (var i=countySelect.options.length;i--;i>=0) {
// compare to #query_edit_HevacCounter.county#
if (countySelect.options[i].text == 'Fulton') {
// on match, set selectedIndex and defaultValue of countySelect
countySelect.selectedIndex = i;
countySelect.defaultValue = 'Fulton';
}
}
countySelect.onchange();
// loop over citySelect options
for (var j=citySelect.options.length;j--;j>=0) {
//compare to #query_edit_HevacCounter.city#
if (citySelect.options[j].text == 'Alpharetta') {
// on match, set selectedIndex and defaultValue of citySelect
citySelect.selectedIndex = j;
citySelect.defaultValue = 'Alpharetta';
}
}
}
window.onload = pageInit;
</script>
</head>
<body>
<form>
<!-- ... -->
<label for="_County">County: (#county#)</label>
<select name="_County" id="_County"></select>
<label for="_City">City: (#city#)</label>
<select name="_City" id="_City"></select>
<!-- ... -->
</form>
</body>
TIA,
Choop.