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 7897653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:13:17+00:00 2026-06-03T08:13:17+00:00

I’ve made a search engine for my database table where the search query gets

  • 0

I’ve made a search engine for my database table where the search query gets sent by Jquery AJAX to a file called search.php. Search.php then sends the result back to the Javascript file where the results are processed and added to index.php.

Everything works perfectly except when I try to add arrow key navigation. For example, I want the first item in the search results page to be selected (by appending the class, red to it and using focus()) when I press the down arrow key, the second result to be selected when I press it again, etc..

Nothing gets selected when I press either the up or down arrow key. I’m testing this in Google Chrome.

index.php:

<link rel="stylesheet" type="text/css" href="search.css"> 

Start searching: <input type="text" id="search" autocomplete="off">
<div id="search_results">
</div>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search.js"></script>

javascript file:

   $(document).ready(function(){

    search_x = $('#search').offset().left;
    search_y = $('#search').offset().top;
    search_height = $('#search').height();
    $('#search_results').css({
        'left': search_x,
        'top': (search_y + search_height + 5)
    });

    //arrow key navigation
    start = -1;
    $(document).on('keydown',document,function(e){
        if(e.keyCode == 38){

        if (start == -1){
            start = ($('#search_results ul li a').size() - 1);  
        }else{
            start--;
            if (start < 0){
                start = ($('#search_results ul li a').size() - 1);  
            }
        }
        $('#search_results ul li a').removeClass('red').focus();
        $('#search_results ul li a').eq(start).addClass('red').focus();

        }

        if(e.keyCode == 40){

        if (start == -1){
            start = 0;  
        }else{
            start++;
            if (start > ($('#search_results ul li a').size() - 1)){
                start = 0;  
            }
        }
        $('#search_results ul li a').removeClass('red').focus();
        $('#search_results ul li a').eq(start).addClass('red').focus();


        }
    });

    $('#search').on(
    'keyup keydown',
    function(){
        var search_term = $(this).val();

        $.post(
        'search.php',
        {
        search_term : search_term       

        },function(data){
            if (data == "nothing"){
                $('#search_results').fadeOut();
            } else {
                    $('#search_results').html(data).fadeIn();
                }
        }); 
     });
  });

part of the search.php file that sends data to index.php:

if (!empty($search_term))
{
        echo '<ul>';
    while ($results_row = mysql_fetch_assoc($search)){
        echo '<li><a href="#">
        <p><strong>',$results_row['place'],'</strong><br>',$results_row['description'],'</p>
        </a>
        </li>';

    }
    echo '</ul>';
} else {
        echo "nothing";
}
  • 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-03T08:13:19+00:00Added an answer on June 3, 2026 at 8:13 am

    After finding some free time to code again, I changed $(document).on('keydown',document,function(e){
    //stuff
    });
    to $(document).on('keydown','#search',function(e){
    //stuff
    });

    The key press event listener was applied to the text field, #search. The code seemed to work after making the change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

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.