Blog
Make jrails_auto_complete work with Safari (2009-10-14)
In the process of migrating an application from Prototype to jQuery, we used jRails as an intermediary step for our rjs templates. As a replacement to Rails’ autocomplete plugin we tried Marty Zalega’s jrails_auto_complete plugin. Unfortunately, there was a problem when using it with Safari: we were not able to move up and down the suggestion list using the up and down keys.
The plugin listens to keypress events instead of keydown events to decide when the up and down keys are pressed. Since Safari 3.1 a keypress event is no longer fired when a non-character key is pressed. We found the explanation in this Stack Overflow thread. The solution is not explicitly shown over there, so here it is…
In the jrails.autocomplete.js file, replace the call to keypress near line 61 with a call to keydown:
1 $(this.update).hide(); 2 $(this.element).attr('autocomplete', 'off') 3 .blur(function(e) { autocomplete.onBlur(e); }) 4 // .keypress(function(e) { autocomplete.onKeyPress(e); }); 5 // Use keydown here. Safari >= 3.1 does fire keypress for 6 // non-character keypresses 7 .keydown(function(e) { autocomplete.onKeyPress(e); }); 8 }
We tested the fix in Safari 4, Firefox 3, Internet Explorer 7 and 8.
For some background info, see John Resig’s post on that subject. There is also a nice writeup on Quirksmode describing the uses of both events.
» posted by Alexandre Grégoire on 2009-10-14 - permalink