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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:12:27+00:00 2026-05-13T21:12:27+00:00

i’ve used .load() with jquery to call a php file that will return search

  • 0

i’ve used .load() with jquery to call a php file that will return search results back into a div below a search input bar, but in the div, using TAB or down arrow will not cycle through the results.

It’s apparent the key controls don’t know the new div of results exists.

What am i looking for exactly to fix this?

I’ve called my php file that returns results from mysql db with the following code

$("#searchinput").keydown(function(e) {
if(e.which == 8) {
    SearchText = $("#searchinput").val().substring(0,$("#searchinput").val().length-1);   
 } 

$("#searchresults").load("inc/ajax_search.php", { searchinput:SearchText });
    $("#searchresults").slideDown();
  • 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-05-13T21:12:27+00:00Added an answer on May 13, 2026 at 9:12 pm

    As cruxst suggests, there are plugins for this kind of thing, but maybe you don’t want to use a plugin. Perhaps you are trying to figure it out for yourself.

    Here is a piece of code that implements a very simple autocomplete. It’s not a plugin and it’s pretty basic but it has the basic features.

    Your question suggests that the function that you may be interested in is highlightResult.

    Below the script is the markup and style required.

    $(function() {
    
        // OPTIONS
        // length of time, in milliseconds, to wait after a keystroke before the search happens
        // usually 250 ~ 400ms
        var wait     = 250;
        // the minimum number of characters required to search on
        var minChars = 3;
        var queryScript = 'inc/ajax_search.php';
    
        // cache our two main players for efficiency
        var $input = $('#searchinput');
        var $output = $('#searchresults' );
    
        // to store the timeout
        var delay = null;
    
        $input.keypress( function( e ) {
            switch( e.keyCode ) {
                case 13:        // return
                    e.preventDefault();
                    selectResult();
                    return;
                case 38:        // up
                    e.preventDefault();
                    highlightResult( -1 );
                    return;
                case 40:        // down
                    e.preventDefault();
                    highlightResult( 1 );
                    return;
                //case ... there may be keys that you want to ignore like left and right
            }
            clearTimeout( delay );
            delay = setTimeout( search, wait );
        });
    
    
        function search() {
            var query = $.trim( $input.val() );
            if( query && query.length >= minChars ) {
                $output.load( queryScript, { searchinput: query } );
            }
        }
    
        function highlightResult( n ) {
    
            if( Math.abs(n) != 1 ) return;
    
            var hilgt = $output.find( '.highlight' );
            var newhilgt;
    
            // if we don't have a highlighted element...
            if( !hilgt.length ) {
                if( n > 0 ) {
                    // highlight the first one
                    $output.find('li').eq(0).addClass('highlight');
                    // jq 1.4+
                    //$output.find('li').first().addClass('highlight');
                } else {
                    // highlight the last one
                    $output.find('li').slice( -1 ).addClass('highlight');
                    // jq 1.4+
                    //$output.find('li').last().addClass('highlight');
                }
            }
    
            if( n > 0 ) {
                var newhilgt = hilgt.next();
            } else {
                var newhilgt = hilgt.prev();
            }
    
            if( newhilgt.length ) {
                newhilgt.addClass('highlight');
                hilgt.removeClass('highlight');
            }
    
        }
    
        function selectResult() {
            var sel = $output.find( '.highlight' );
            if( sel.length ) {
                // do something with the selected element
                console.log( sel[0] );
            }
        }
    
        // For completeness the functions below handle mouse stuff
        $('#searchresults li').live( 'mouseover', function( e ) {
            $(this).siblings('.highlight').removeClass('highlight');
            $(this).addClass('highlight');
        });
    
        $('#searchresults li').live( 'click', function( e ) {
            e.preventDefault();
            selectResult();
        })
    
    });
    

    Requirements

    <style type="text/css">
        .highlight {
            background: red;
        }
    </style>
    
    <input type="text" id="searchinput"/>
    <div id="searchresults"></div>
    

    Return from your ajax call assumed to be something like

    <ul>
        <li>result 1</li>
        <li>result 2</li>
        <li>result 3</li>
        ...
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because shells were not always entirely well-behaved if one of… May 15, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer Looks like musikk beat me to an explanation, but here's… May 15, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer If you really want one big regex it would be… May 15, 2026 at 2:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.