JSFiddle - React, Tailwind, and code Playground
HTML
<input id="input_city" type="text" />
<div class="suggestions">
<ul>
<li value="0" name="New York">New York</li>
<li value="1" name="London">London</li>
<li value="2" name="Paris">Paris</li>
<li value="3" name="Sydney">Sydney</li>
</ul>
</div>
CSS
ul {
padding: 50px 0;
}
li {
padding: 6px 30px;
}
.selected {
background: #ccc;
}
JavaScript
var $listItems = $('li');
$('input').keydown(function(e)
{
var key = e.keyCode,
$selected = $listItems.filter('.selected'),
$current;
if ( key != 40 && key != 38 ) return;
$listItems.removeClass('selected');
if ( key == 40 ) // Down key
{
if ( ! $selected.length || $selected.is(':last-child') ) {
$current = $listItems.eq(0);
}
else {
$current = $selected.next();
}
}
else if ( key == 38 ) // Up key
{
if ( ! $selected.length || $selected.is(':first-child') ) {
$current = $listItems.last();
}
else {
$current = $selected.prev();
}
}
$current.addClass('selected');
});