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.

http://jsfiddle.net/uu6kg/

$("#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(); }); 

click list of key codes

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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -