0

I am a newbie on jQuery and was building an auto-suggest box for my website. I used the following HTML:

<input type="text" name="suggest" class="autosuggest1">
<div class="dropdown">
  <ul class="result1">
  </ul>
</div>

I have added elements to ul list on the go by the following jQuery code:-

$(document).ready(function () {
    $('.autosuggest1').keyup(function () {
        var search_term = $(this).val();
        $.post("search.php", {
                search_term: search_term
            }, function (data) {
                $('.result1').html(data);
                $('.result1 li').click(function () {
                    var result = $(this).text();
                    $('.autosuggest1').val(result);
                    $('.result1').html('');
                });
            });
    });
});

My problem is that the element is selected only by clicking it and I want it to get also selected by arrow keys on keyboard. I can't figure how.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
apsdehal
  • 845
  • 2
  • 10
  • 27

3 Answers3

0

Why don't you use jQuery UI's autocomplete? Reasons why you should choose this :

  • It comes with all the functionality you will ever need. In your case, you need to control the results like with your keyboard, which already exists.
  • Please don't re-invent the wheel. Autocomplete has been in development for many years and you'd be wasting your time by re-building that widget from scratch.

You'd initialize autocomplete on a text box like this :

$("#city").autocomplete(options)

Here's a demo : http://jsfiddle.net/hungerpain/FqqxY/

Type any city name and check for yourself :)

krishwader
  • 11,341
  • 1
  • 34
  • 51
0

I definitely agree with @passionateCoders answer using autocomplete, but if you must do it the way you asked then this Fiddle should help (adapted from the SO answer to a similar question).

JSFiddle

<input type="text" name="suggest" class="autosuggest1">
<div class="dropdown">
    <ul class="result1">          
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</div>

 <script>
 var chosen = "";
 $(document).keydown(function(e){ // 38-up, 40-down
   if (e.keyCode == 40) { 
    if(chosen === "") {
        chosen = 0;
    } else if((chosen+1) < $('li').length) {
        chosen++; 
    }
    $('li').removeClass('selected');
    $('li:eq('+chosen+')').addClass('selected');
    var result = $('li:eq('+chosen+')').text();
    $('.autosuggest1').val(result);  
    return false;
}
if (e.keyCode == 38) { 
    if(chosen === "") {
        chosen = 0;
    } else if(chosen > 0) {
        chosen--;            
    }
    $('li').removeClass('selected');
    $('li:eq('+chosen+')').addClass('selected');
    var result = $('li:eq('+chosen+')').text();
    $('.autosuggest1').val(result);  
    return false;
 }
 });
 </script>
Community
  • 1
  • 1
proggrock
  • 3,239
  • 6
  • 36
  • 51
0

This is an old post. However I found it helpful now for me since my requirement is the same as yours.

The answer from progrock works well. The only additional item you may have to add is a handle in the keyup function to explicitly handle the Keyboard arrow key events. Not sure if you already have added it.

$('.autosuggest1').keyup(function () {
     if (e.keyCode == 40 || e.keyCode == 38) {return false;}
     ...
});

Posting this here so that it might be helpful for others to use, if they face the same issue.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Sriram
  • 55
  • 1
  • 9