Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Last active December 14, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjaap/5128851 to your computer and use it in GitHub Desktop.
Save peterjaap/5128851 to your computer and use it in GitHub Desktop.
Prototype javascript thingy to autofocus on search element on typing (ignoring navigational keys) and setting the search box as the first tab index
Event.observe(window,'load',function() {
$('search').tabIndex = 1;
$(document).observe('keyup', function(e) {
var navKeys = new Array(Event.KEY_BACKSPACE,Event.KEY_TAB,Event.KEY_RETURN,Event.KEY_ESC,Event.KEY_LEFT,Event.KEY_UP,Event.KEY_RIGHT,Event.KEY_DOWN,Event.KEY_DELETE,Event.KEY_HOME,Event.KEY_END,Event.KEY_PAGEUP,Event.KEY_PAGEDOWN,Event.KEY_INSERT);
if(Event.element(e) == document.body) {
if(navKeys.indexOf(e.keyCode)==-1){
$('search').focus();
$('search').value = String.fromCharCode(e.keyCode).toLowerCase();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment