Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Autocomplete problem - Shift Key behaves same as Return Key

See: http://www.airbnb.com/

In the search bar, start typing "san f" (no quotes, all lowercase), then hit Return (or Enter). "San Francisco" is autocompleted. This is good!

Now clear the search field and start over. type "San F" and boom - "San Francisco" is autocompleted as soon as you hit Shift. This is not expected.

This happens in FF & Safari, but is untested elsewhere. I've looked through the jQuery Autocomplete Source Code and everything looks normal.

Has anyone experienced this before?

like image 250
user237005 Avatar asked Aug 16 '26 20:08

user237005


2 Answers

Try Search option in autocomplete as follow:

$(function() {
var availableTags = [
  "ActionScript",
  "AppleScript",
  "Asp",
  "BASIC",
  "C",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
  "Groovy",
  "Haskell",
  "Java",
  "JavaScript",
  "Lisp",
  "Perl",
  "PHP",
  "Python",
  "Ruby",
  "Scala",
  "Scheme"
];
$( "#tags" ).autocomplete({
  source: availableTags,
    search: function( event, ui ) {
       if(event.which == 16 || event.which == 17 )
        event.preventDefault();  
    }
});
like image 112
Code Spy Avatar answered Aug 18 '26 11:08

Code Spy


To get around this issue:

To the KEY object add:

    SHIFT: 16,
    CTRL: 17,
    ALT: 18

And to the very top of the onChange function add:

if ( jQuery.inArray(lastKeyPressCode, [KEY.SHIFT, KEY.CTRL,
       KEY.ALT]) !== -1 )
                return; 
like image 39
Glennular Avatar answered Aug 18 '26 10:08

Glennular