searching SELECT list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian

    searching SELECT list

    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>






  • HikksNotAtHome

    #2
    Re: searching SELECT list

    In article <bi079v$vcf$1@n ews.tdl.com>, "Adrian" <adrian@nocrapp lease.ascc.com>
    writes:
    [color=blue]
    >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?[/color]

    In which browser? IE alerts me that event is undefined.
    Mozilla tells me event is undefined in the Javascript Console. Although it does
    go ahead and take me to the key I pressed (within reason, typing aaa took me to
    abc, not to aaa).
    [color=blue]
    > if ((keyCode == "8") || (keyCode == "46"))
    >
    > // Delete or backspace decrement the searchString
    > searchString = searchString.sl ice(0,-1);[/color]

    Just a sidenote:
    The delete key and the backspace key do totally different things. Backspace
    removes the character prior to cursor position, delete removes the character
    after the current cursor position.

    MySearch<backsp ace>String
    results in MySearcString
    MySearch<delete >String
    results in MySearchtring

    Nothing to do with your original problem. Just something I noticed in scanning
    the code.
    --
    Randy
    All code posted is dependent upon the viewing browser
    supporting the methods called, and Javascript being enabled.

    Comment

    Working...