Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:18:33+00:00 2026-06-12T02:18:33+00:00

I cant figure out how to make the arrow keys work for my autosuggest

  • 0

I cant figure out how to make the arrow keys work for my autosuggest box. I know I need to add something like this to my code:

    var keynum = 0; 
    if(window.event) { keynum = e.keyCode; }  // IE  
    else if(e.which) { keynum = e.which; }    // Netscape/Firefox/Opera 
    if(keynum === 38) { // up 
    //Move selection up 
    } 
    if(keynum === 40) { // down 
    //Move selection down 
    } 
    if(keynum === 13) { // enter 
    //Act on current selection 
    } 

But I have no idea how to fit that in with my code. Can someone help me figure out how this would go with my code. when i press the down arrow i want the highlighted result to move down one and up when the up arrow is pressed. Heres my code:

   </ul>

<div class="input-wrapper">
<input type="text" class="autosuggest" value="Type in a city or town" onblur="onBlur (this)" onfocus="onFocus(this)" > <input type="submit" value="Search">

<div class="dropdown">
    <ul class="result"></ul>


</div>
</div>

      $(document).ready(function() {
$('.autosuggest').keyup(function() {
var search_term = $(this).attr('value');

$.post('php/search.php', { search_term: search_term }, function(data) {
    $('.result').html(data);

$('.result li').click(function(){
    var result_value = $(this).text();
    $('.autosuggest').attr('value', result_value);
    $('.result').html('');

    });

    });

});
      });
       function onBlur(el) { 
if (el.value == '') { 
    el.value = el.defaultValue; 
} 
} 
 function onFocus(el) { 
if (el.value == el.defaultValue) { 
    el.value = ''; 
} 
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:18:34+00:00Added an answer on June 12, 2026 at 2:18 am

    I noticed you’re already using jQuery. If possible, the jquery-ui library already provides the keyboard functionality you want. Including the ajax datasource. type ‘j’ into the jQuery-ui demo here and use you’re arrow keys. Then look at the documentation to pull in your ajax post.

    example js

    $(function(){
      $(".autosuggest").autocomplete({
                source: function( request, response ) {
                    $.ajax({
                        url: "/php/searchToJSON.php",
                        dataType: "json",
                        data: {
                            search_term: request.term
                        },
                        success: function( data ) {
    //this maps your serverside /searchToJSON.php to the {label:something, value:somethingElseMaybe} that jquery-ui autocomplete needs
                            response( $.map( data.SomeNameYouPick, function( itemWithinSomeNameYouPick ) {
                                return {
                                    label: itemWithinSomeNameYouPick.labelToShowForOneItem,
                                    value: itemWithinSomeNameYouPick.valueWhenSelectedForOneItem
                                }
                            }));
                        }
                    });
                },
                minLength: 2
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems in my head like it should work but I cant figure out
I want to do something but cant figure out how to do this (i
I know this question has been asked countless times, but I cant figure out
I am getting segmentation fault in this code but i cant figure out why.
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't
This is what I want but cant figure out how to. $a is this
I cant figure out how to proceed the best way with this problem. Right
I know this should be fairly easy but I can't quite figure it out
im trying to do this i just cant figure out how to any ideas?
http://jsfiddle.net/DaJWC/ i can't figure out how to make one of the links reverse so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.