MrCBofBCinTX
Technical User
I am generating two select lists from perl.
One shows a class drop down list.
The second one shows a subclass drop down list.
To make life easier, this makes only subclasses that are in a class show up after selecting a class.
Works good. I just rewrote the perl subroutine that generates this to be much faster.
Good!
Now I want to see if there is a better way to write the javascript part.
What I have is very long and will only get longer as more classes and subclasses are added.
I am not a Javascript programmer (cut and paste), so any advice will be helpful.
I have another set of category and subcategory drop downs I would like to tie together like these two, but reasonable speed is necessary.
One shows a class drop down list.
The second one shows a subclass drop down list.
To make life easier, this makes only subclasses that are in a class show up after selecting a class.
Works good. I just rewrote the perl subroutine that generates this to be much faster.
Good!
Now I want to see if there is a better way to write the javascript part.
What I have is very long and will only get longer as more classes and subclasses are added.
I am not a Javascript programmer (cut and paste), so any advice will be helpful.
I have another set of category and subcategory drop downs I would like to tie together like these two, but reasonable speed is necessary.
JavaScript:
<script type="text/javascript">
//<![CDATA[
if (window.addEventListener) {
window.addEventListener("load",setupEventsC,false);
} else if (window.attachEvent) {
window.attachEvent("onload",setupEventsC);
} else {
window.onload=setupEventsC;
}
function setupEventsC(evnt) {
var opts = document.getElementById("someForm").class_selected.options;
var subclass_selected = document.getElementById("someForm").subclass_selected.options;
subclass_selected[subclass_selected.length] = new Option("All", "All");
subclass_selected[subclass_selected.length] = new Option("Construction", "Construction");
subclass_selected[subclass_selected.length] = new Option("Floor", "Floor");
subclass_selected[subclass_selected.length] = new Option("Misc", "Misc");
subclass_selected[subclass_selected.length] = new Option("Wallpaper", "Wallpaper");
subclass_selected[subclass_selected.length] = new Option("Wood", "Wood");
subclass_selected[subclass_selected.length] = new Option("Storage", "Storage");
subclass_selected[subclass_selected.length] = new Option("Aluminum", "Aluminum");
subclass_selected[subclass_selected.length] = new Option("Steel", "Steel");
subclass_selected[subclass_selected.length] = new Option("Wood", "Wood");
subclass_selected[subclass_selected.length] = new Option("Panels", "Panels");
subclass_selected[subclass_selected.length] = new Option("Sinks", "Sinks");
subclass_selected[subclass_selected.length] = new Option("Adhesive", "Adhesive");
etc...
document.getElementById("someForm").class_selected.onchange = checkSelectC;
}
function checkSelectC(evnt) {
var opts = document.getElementById("someForm").class_selected.options;
var subclass_selected = document.getElementById("someForm").subclass_selected.options;
for ( var i = 0; i < opts.length; i++) {
if ( opts[i].selected ) {
switch(opts[i].value) {
case 'All' :
subclass_selected[subclass_selected.length] = new Option("Construction", "Construction");
subclass_selected[subclass_selected.length] = new Option("Floor", "Floor");
subclass_selected[subclass_selected.length] = new Option("Misc", "Misc");
subclass_selected[subclass_selected.length] = new Option("Wallpaper", "Wallpaper");
subclass_selected[subclass_selected.length] = new Option("Wood", "Wood");
subclass_selected[subclass_selected.length] = new Option("Storage", "Storage");
subclass_selected[subclass_selected.length] = new Option("Aluminum", "Aluminum");
subclass_selected[subclass_selected.length] = new Option("Steel", "Steel");
subclass_selected[subclass_selected.length] = new Option("Wood", "Wood");
subclass_selected[subclass_selected.length] = new Option("Panels", "Panels");
subclass_selected[subclass_selected.length] = new Option("Sinks", "Sinks");
subclass_selected[subclass_selected.length] = new Option("Adhesive", "Adhesive");
subclass_selected[subclass_selected.length] = new Option("Silicone", "Silicone");
subclass_selected[subclass_selected.length] = new Option("Electric", "Electric");
etc..
break;
case 'Adhesive' :
subclass_selected.length = 0;
subclass_selected[subclass_selected.length] = new Option("All", "All");
subclass_selected[subclass_selected.length] = new Option("Construction", "Construction");
subclass_selected[subclass_selected.length] = new Option("Floor", "Floor");
subclass_selected[subclass_selected.length] = new Option("Misc", "Misc");
subclass_selected[subclass_selected.length] = new Option("Wallpaper", "Wallpaper");
subclass_selected[subclass_selected.length] = new Option("Wood", "Wood");
break;
case 'Attic' :
subclass_selected.length = 0;
subclass_selected[subclass_selected.length] = new Option("All", "All");
subclass_selected[subclass_selected.length] = new Option("Storage", "Storage");
break;
etc..
}
}
}
return false;
}
</script>