<select> box

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

    <select> box

    I have a select box with a lot of entries in it, some of which start with
    the same letter.

    I want the user to be able to type the first few letters into the select box
    and have the list "move" to the appropriate place.

    any one know how to achieve this?

    TIA
    Steve


  • Vincent van Beveren

    #2
    Re: &lt;select&g t; box

    Unfortunatly the select box isn't editable in most browsers (if it can
    be made editable, I didn't know) However, there is kind of a solution
    for this. The most simpelest (and inefficient way) of searching for
    a string and selecting it could be this:

    function seek(str) {
    select = document.getEle mentById("selec tor");
    for (i=0;i<select.o ptions.length;i ++) {
    v = select.options[i].value;
    if (v.length>=str. length) {
    if (v.substring(0, str.length)==st r) {
    select.options[i].selected = true;
    return;
    }
    }
    }
    return;
    }

    Suppose your code looks like this

    <SELECT ID="selector" onKeyPress="jum p(event);">
    </SELECT>

    Then you could handle the onKeyPress event like this.

    str = "";

    function jump(e) {
    e = (e==null?window .event:e);

    if (e.keyCode==8) { // backspace
    str=str.substri ng(0, str.length-1);
    } else {
    // don't know exactly if that is the right function
    key = String.fromChar Code(e.keyCode) ;
    if (key>='A' &&.key=<'Z') {
    str+=key;
    }
    }
    seek(str);
    }


    Something like that should work. I didn;t try it though.

    Good luck,
    Vincent

    Steve Wright wrote:
    [color=blue]
    > I have a select box with a lot of entries in it, some of which start with
    > the same letter.
    >
    > I want the user to be able to type the first few letters into the select box
    > and have the list "move" to the appropriate place.
    >
    > any one know how to achieve this?
    >
    > TIA
    > Steve
    >
    >[/color]

    Comment

    • bruce

      #3
      Re: &lt;select&g t; box

      "Steve Wright" <wright@wcc.gov t.nz> wrote in message news:<108692129 3.20923@muldoon >...[color=blue]
      > I have a select box with a lot of entries in it, some of which start with
      > the same letter.
      >
      > I want the user to be able to type the first few letters into the select box
      > and have the list "move" to the appropriate place.
      >
      > any one know how to achieve this?
      >
      > TIA
      > Steve[/color]

      Without getting into detailed coding, here would be my first
      attempt:
      1. Have a global variable, keeping track of the letters typed
      (call it vKeysPressed for this example)
      2. In the body tag, have an event handler
      "onkeypressed=" Gatherkeys()".
      3. in "Gatherkeys ()" you would concatenate the key pressed to the
      variable vKeysPressed. use the event.keyCode to find out what key is
      pressed.
      4. Now, using vKeysPressed, search through your select control
      for the item which matches vKeysPressed.

      Comment

      • Ux

        #4
        Re: &lt;select&g t; box

        Steve Wright wrote:[color=blue]
        > I have a select box with a lot of entries in it, some of which start with
        > the same letter.
        >
        > I want the user to be able to type the first few letters into the select box
        > and have the list "move" to the appropriate place.
        >
        > any one know how to achieve this?
        >
        > TIA
        > Steve
        >
        >[/color]

        Do you really need your user type letters INTO THE SELECT BOX ?
        I find this not very clear for users because of the backspaces, del,
        arrowkey they can type without to see the complete word actually inserted.
        IF you change your mind and decide that the user can type input in a
        textbox that drives the selectbox positioning then answer this post.
        I've just solved the same problem with a binary search on the options
        array. Very fast even on 10000 entry (entry must be sorted).

        Ciao
        Marco

        Comment

        Working...