Results 1 to 5 of 5

Thread: Predictive Text Select Menu

  1. #1
    Join Date
    Sep 2005
    Location
    Brisbane, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Predictive Text Select Menu

    Hi,

    I'm working on something which will have a form containing a select menu with a lot of items (around 500). I would like to have a text box next to the list with predictive text input, so when you start typing in a text box it will choose the first item in the list starting with what you've typed so far.

    For example, if this is the drop down list...:
    • Aadvark
    • Cat
    • Dog
    • Echidna
    • Elephant
    • Emu
    • Frog
    • Zebra

    ... and you type "el" in the box, it will choose Elephant. Also, if you do decide to click in the list, it will put the item you choose into the text box. And possibly pop up an alert when you leave the text box, if you typed something that doesn't match anything in the list.

    Can somebody please help me with this, particularly the predictive text bit.

    Thanks,
    Brendan

  2. #2
    Join Date
    Sep 2005
    Location
    Brisbane, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh yeh, if there's a way to make it in one thing (ie. you can type straight into the select menu) that would be good, but I wouldn't think that's possible...

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's already done by the browser. Try it: create a select box, give it focus, and type something.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2005
    Location
    Brisbane, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It seems to work in Firefox, but in IE it only works for the first letter. When you type the next letter it goes to the items starting with that letter.

    The way I was thinking was kinda like in Access, where you type a letter and it has the next word in the list, with the rest of it highlighted, you keep typing and it chooses the first word starting with what you've typed. But it doesn't have to be like that, the browser's way should be fine.

  5. #5
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    There are a couple of ones out there. Deadbeef seems to be quite good but is only supported by IE:

    http://www.deadbeef.com/index.php/2005/08/04/title

    What i use sometimes is this code as you can pass the value over and not just the name as in the one above:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Test page</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    script language="javascript">
    //Autocomplete textfield

    function matchFieldSelect (fieldselectvalue) {
      var 
    property value 'value' 'text';
      var 
    found false;
      for (var 
    0select.options.lengthi++)
        if ((
    found select.options[i][property].indexOf(field.value) ==
    0))
          break;
      if (
    found)
        
    select.selectedIndex i;
      else
        
    select.selectedIndex = -1;
      if (
    field.createTextRange) {
        var 
    cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;"
        
    if (cursorKeys.indexOf(event.keyCode+";") == -1) {
          var 
    r1 field.createTextRange()
          var 
    oldValue r1.text;
          var 
    newValue found select.options[i][property] : oldValue;
          if (
    newValue != field.value) {
            
    field.value newValue
            
    var rNew field.createTextRange()
            
    rNew.moveStart('character'oldValue.length)
            
    rNew.select()
          }
        }
      }
    }

    </script>
    </head>

    <body>

    <form>
    <input type="text" name="a" onkeyup="matchFieldSelect(this, this.form.b)" />

    <select name="b" size="1">
     <option value="1">aa</option>
     <option value="2">bb</option>
     <option value="3">cc</option>
     <option value="4">dd</option>
    </select>

    </form>
    </body>
    </html> 
    Last edited by mattster; 09-06-2005 at 09:33 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •