Autocomplete pageup/pagedown key events
I have an autosuggest feature where i want to enable key functionality to scroll up and down through the dropdown list. heres my code
- $(document).ready(function() {
- $('.dropdown').hide();
- $('.autosuggest').keyup(function (e) {
- isDelete = false;
- // if delete or backspace were pressed,
- // less than 3 characters also trigger suggestion
- if( e.which == 8 || e.which == 46) {
- isDelete = true;;
- }
- var search_term = $(this).attr('value');
- if(
- search_term.length == 0 ||
- (search_term.length < 1 && isDelete == false)
- ) {
- $('.dropdown').hide();
- } else {
- $('.dropdown').show();
- }
- if(search_term.length >= 1 || isDelete == true) {
- var category=$('#category').attr('value');
- var conditions = {
- 'search_term' : search_term,
- 'category' : category
- };
- $.post('ajax/searchApp.php', conditions, function(data) {
- $('.result').html(data);
- $('.result li').click(function() {
- var result_value=$(this).text();
- $('.autosuggest').attr('value',result_value);
- //alert(result_value);
- $('.result').html('');
- });
- });
- };
- });
- $('result').keydown(function(e){
- if (e.keyCode == 38) {
- $(this).prev().focus();
- }else if (e.keyCode == 40) {
- $(this).next().focus();
- }
- });
- });