Hello all,
I'm working on an autosuggest plugin with jQuery, and I'd like to add Keypress events so that users can arrow down and up. Also, if they press enter it will add the value to the input.
This is what my HTML looks like:
<input id="myInput" class="textInput" type="text" name="instructorName"/>
<label for="search"> </label>
<div id="myInputResults" class="results" style="display: block;">
<ul>
<li><a><div class="suggClass">Suggestion #1</div></a></li>
<li><a><div class="suggClass">Suggestion #2</div></a></li>
<li><a><div class="suggClass">Suggestion #3</div></a></li>
<li><a><div class="suggClass">Suggestion #4</div></a></li>
</ul>
</div>
So far, I have something like this:
$("#myInput").keyup(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 40) { //If user "keys down"
//I would want to addClass 'hovered' to the "first <li>"
// Remove 'hovered' class from any other <li>'s
}
});
Up to this point, I'm not quite sure on the proper logic to use so that users can scroll up, down, and press 'enter' to select an item.
Any suggestions and help on this would be greatly appreciated! Thank you.