javascript - jQuery Key Events with a Search Box -


I am working on an automatic advice plugin with jQuery, and I would like to add a keypress event so that the user down arrow Able and up In addition, if they enter, it will add value to the input.

This looks like my HTML:

   & Lt; Label = "search" & gt; & Lt; / Labels & gt; & Lt; Div id = "myInputResults" class = "results" style = "display: block;" & Gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; A & gt; Div class = "recommended class" & gt; Suggestion # 1 & lt; / Div & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A & gt; Div class = "suggest class" & gt; Suggestion # 2 & lt; / Div & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A & gt; Div class = "suggest class" & gt; Suggestion # 3 & lt; / Div & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A & gt; Div class = "recommended class" & gt; Suggestion # 4 & lt; / Div & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Div & gt;  

So far, I have something like this:

  $ ("# myInput"). Key (function (e) {var code = (e.keycode? E.keycode: e.which); if (code == 40) {// if user "key down" // I "first & lieutenant;   

By this point, I am not sure whether to use Users can scroll, down, and press 'Enter' to select an item for proper reason.

Any suggestions and support on this will be greatly appreciated! Thank you. / P>

< V class = "post-text" itemprop = "text">

Adding on Psytronic's code, it should also cover the "Enter" keycode ...

  $ (function ( ) {$ ("# MyInput") .keyp (function (e) {var code = (e.keyCode? E. Kikode: E.H.H.); if (code == 40) {if ($ (" Li.hovered "). Length == 0) {$ (" # myInputResults li "). Eq (0) .addClass (" hovered ");} and {$ (" ("(" ") "removeClass" ("hovered" Next (). AddClass ("hovered");}} if (code == 13) {if ($ ("li.hovered") .length & gt;) {$ ("# myInput"). Val ($ ("Li.hovered"). Eq (0) .find ("div.suggClass"). Eq (0) .text ()); }}}}}}};  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -