html - Jquery keypress event ignore arrow keys -
i have input box acts search (similar facebook). using jquery on key-press event works fine. problem can't scroll down on set of results using arrow keys, every time 1 of them pressed search results being regenerated. possible ignore arrow keys / shift / tab etc.
$("#search-form .search-terms").on("keypress", function(){ $("#search-items #autocom").fadein(); });
thanks
you need filter out arrow key codes (37,38,39,40), try this:
note function(e)
in place of function()
- allows grab event , therefore key code.
$('#search-form .search-terms').on('keydown', function(e){ // keycode of current keypress event var code = (e.keycode || e.which); // nothing if it's arrow key if(code == 37 || code == 38 || code == 39 || code == 40) { return; } // normal behaviour other key $('#search-items #autocom').fadein(); });
a note docs on keypress
/keyup
/keydown
:
note keydown , keyup provide code indicating key pressed, while keypress indicates character entered. example, lowercase "a" reported 65 keydown , keyup, 97 keypress. uppercase "a" reported 65 events. because of distinction, when catching special keystrokes such arrow keys, .keydown() or .keyup() better choice.
the keypress
event works in almost situations it's prudent use keyup
or keydown
because browsers (i think older version of firefox) don't detect keys, such arrow keys, using keypress
event.
Comments
Post a Comment