This script works well for searching thru a SELECT list but doesnt work so
well with the MULTIPLE option. I need to modify it to work for a SELECT
MULTIPLE but I have no idea where to begin, any tips?
<script>
var searchString = "";
function clearSearchStri ng() {
searchString = "";
}
function betterSelect(se lectName, formName) {
// Get the keypress from the select box
var keyCode = event.keyCode;
var keyChar = String.fromChar Code(keyCode).t oLowerCase();
var selectBox = eval("document. " + formName + "." + selectName);
var optionsCount = selectBox.optio ns.length;
var i;
// Check to see if the keypress is something we should care about...
if ((keyCode == "8") || (keyCode == "46"))
// Delete or backspace decrement the searchString
searchString = searchString.sl ice(0,-1);
} else if ((keyCode == "38") || (keyCode == "37")) {
// Up or left arrow move to the preceeding item in options list
if (selectBox.sele ctedIndex > 0) {
selectBox.selec tedIndex --; }
return false;
} else if ((keyCode == "40") || (keyCode == "39")) {
// Down or right arrow move to the next item in the options list
if (selectBox.sele ctedIndex < (optionsCount - 1)) {
selectBox.selec tedIndex ++; }
return false;
} else if (keyCode == "27") {
// Escape clears the searchString variable
searchString = "";
window.status = "";
return false;
} else if (keyCode == "188") {
// Comma dealt with manually
searchString = searchString + ",";
} else
// Add the new keypress to the searchString variable (default)
searchString = searchString + keyChar;
}
// Display keypresses in the status bar at the bottom of the window
var statusDisplay = searchString.to UpperCase();
window.status = "Narrowing select menu items (press 'Esc' to clear): " +
statusDisplay;
// Advance to the selected item
for(i = 0 ; i < optionsCount; i++) {
var optionItem = selectBox.optio ns[i].text;
// Uncomment this routine to selectively exclude portions of a substring
(e.g. commas, etc.)
/* var findComma = optionItem.inde xOf(", ");
if (findComma != -1) {
optionItem = optionItem.subs tr(0, findComma) +
optionItem.subs tr(findComma + 2, optionItem.leng th);
} */
var optionItemSubSt ring =
optionItem.subs tring(0,searchS tring.length).t oLowerCase();
// Check each option item for text like the current searchString
if (optionItemSubS tring == searchString) {
// If there is a match, select that item and exit loop
selectBox.optio ns[i].selected = true;
break;
}
}
}
</script>
<FORM ACTION="" METHOD="POST" NAME="summary">
<select name="Customer_ Name" onkeydown="retu rn false;"
onkeyup="better Select('Custome r_Name', 'summary');"
onclick="clearS earchString(); window.status = '';" size="1" >
<option>aaa</option>
<option>bbbb</option>
<option>cc</option>
<option>abc</option>
<option>123</option>
</select>
</FORM>
well with the MULTIPLE option. I need to modify it to work for a SELECT
MULTIPLE but I have no idea where to begin, any tips?
<script>
var searchString = "";
function clearSearchStri ng() {
searchString = "";
}
function betterSelect(se lectName, formName) {
// Get the keypress from the select box
var keyCode = event.keyCode;
var keyChar = String.fromChar Code(keyCode).t oLowerCase();
var selectBox = eval("document. " + formName + "." + selectName);
var optionsCount = selectBox.optio ns.length;
var i;
// Check to see if the keypress is something we should care about...
if ((keyCode == "8") || (keyCode == "46"))
// Delete or backspace decrement the searchString
searchString = searchString.sl ice(0,-1);
} else if ((keyCode == "38") || (keyCode == "37")) {
// Up or left arrow move to the preceeding item in options list
if (selectBox.sele ctedIndex > 0) {
selectBox.selec tedIndex --; }
return false;
} else if ((keyCode == "40") || (keyCode == "39")) {
// Down or right arrow move to the next item in the options list
if (selectBox.sele ctedIndex < (optionsCount - 1)) {
selectBox.selec tedIndex ++; }
return false;
} else if (keyCode == "27") {
// Escape clears the searchString variable
searchString = "";
window.status = "";
return false;
} else if (keyCode == "188") {
// Comma dealt with manually
searchString = searchString + ",";
} else
// Add the new keypress to the searchString variable (default)
searchString = searchString + keyChar;
}
// Display keypresses in the status bar at the bottom of the window
var statusDisplay = searchString.to UpperCase();
window.status = "Narrowing select menu items (press 'Esc' to clear): " +
statusDisplay;
// Advance to the selected item
for(i = 0 ; i < optionsCount; i++) {
var optionItem = selectBox.optio ns[i].text;
// Uncomment this routine to selectively exclude portions of a substring
(e.g. commas, etc.)
/* var findComma = optionItem.inde xOf(", ");
if (findComma != -1) {
optionItem = optionItem.subs tr(0, findComma) +
optionItem.subs tr(findComma + 2, optionItem.leng th);
} */
var optionItemSubSt ring =
optionItem.subs tring(0,searchS tring.length).t oLowerCase();
// Check each option item for text like the current searchString
if (optionItemSubS tring == searchString) {
// If there is a match, select that item and exit loop
selectBox.optio ns[i].selected = true;
break;
}
}
}
</script>
<FORM ACTION="" METHOD="POST" NAME="summary">
<select name="Customer_ Name" onkeydown="retu rn false;"
onkeyup="better Select('Custome r_Name', 'summary');"
onclick="clearS earchString(); window.status = '';" size="1" >
<option>aaa</option>
<option>bbbb</option>
<option>cc</option>
<option>abc</option>
<option>123</option>
</select>
</FORM>
Comment