Last active
December 14, 2015 18:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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