Javascript limit select box
From PikaDocs
Here's some experimental code to use as an alternative to iframes and services/problem-server.php - narrowing one select list based on what is chosen in another. This code works as I tested it, but I am not running it in production because I'm unsure it actually fits with how people use our system.
function limit_select_options (master, slave, numChars, untouched) {
// take numChars number of chars at the beginning of the master field
controlChars = master.value.substring(0,numChars);
// clear the existing options out of the slave select box
slave.options.length = 0;
// get the length of the untouched select box to use in the for loop
var numOptions = untouched.options.length;
var newlength = 0;
var i = 0;
// loop through all the options in the untouched select box
// if an option matches the controlChars, put it back into the slave
for (i=0; i < numOptions; i++) {
if (untouched.options[i]==null)
{} // do nothing, it's the blank choice at the top of the menu
else if (untouched.options[i].value.substring(0,numChars)==controlChars) {
newlength++;
slave.options.length = newlength;
slave.options[newlength] = new Option(untouched.options[i].text,untouched.options[i].value);
}
}
}
// Andrew Cameron 2006/9/21
// first set aside an untouched copy of the select list we are going to limit
// http://developer.mozilla.org/en/docs/DOM:element.cloneNode
var untouchedSelect1 = document.ws.sp_problem.cloneNode(true);
// because of some wierd aspect of javascript, I can't assign the limit_select_options function directly as an onchange event
// instead, use this syntax and wrap the function call in a new Function, and it works. Voila.
var functionCall = "limit_select_options(document.ws.problem, document.ws.sp_problem, 2, untouchedSelect1)";
document.ws.problem.onchange = new Function ("wrapper_function", functionCall);
original entry by Andrew Cameron
Legal Services Lawline of Vermont (http://www.lawlinevt.org/) and Vermont Legal Aid (http://www.vtlegalaid.org/)
