Skip to content

Instantly share code, notes, and snippets.

@phcostabh
Created October 18, 2013 18:45
Show Gist options
  • Select an option

  • Save phcostabh/7046167 to your computer and use it in GitHub Desktop.

Select an option

Save phcostabh/7046167 to your computer and use it in GitHub Desktop.
this.$suggest = $('div.p-suggest');
var navigateSuggestions = function(direction) {
var $active = this.$suggest.find('.active'),
listLenght = this.$suggest.find('ul > li').length,
index;
if ($active.length > 0) {
index = (direction === 38) ? $active.index() - 1 : $active.index() + 1;
console.log(index, listLenght);
if (index === listLenght) {
index = 0;
}
$item = this.$suggest.find('ul > li:eq(' + index + ')');
if ($item.length > 0) {
$active.removeClass('active');
$item.addClass('active');
}
} else {
this.$suggest.find('ul > li:first-child').addClass('active');
}
};
(function($) {
$('input.p-search_input').on('keyup', function(event) {
event.preventDefault();
if (event.keyCode === 38 || event.keyCode === 40) {
navigateSuggestions(event.keyCode);
}
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment