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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:56:09+00:00 2026-06-13T15:56:09+00:00

I have a search box which has an auto-complete feature that I’ve been building.

  • 0

I have a search box which has an auto-complete feature that I’ve been building. I’m having a couple issues.

Two things:

  • When arrowing up and down through the list of auto-completed/search terms, it’s including those which were hidden by the filter. How can I make sure only the visible list items can be traversed?

  • When I hit enter to select one of the items from the list, the dropdown is still there because it is matching the word in the search box.

FIDDLE

Some code:

$(document).ready(function() {
    $("#dropdown").hide();
    $("input").keyup(function() {
        if (this.value.length) {
            var that = this;
            $("#dropdown li").hide().filter(function() {
                return $(this).html().toLowerCase().indexOf(that.value.toLowerCase()) !== -1;
            }).show();
            $("#dropdown").show();
        }
        else {
            $("#dropdown").hide();
        }
    });

    $('li').click(function() {
        $('#search').val($(this).text());
        $("#dropdown").hide();
    });

    var li = $('li');
    var liSelected;

    $('input').keydown(function(e) {
        if(e.which === 40) {
            if(liSelected) {
                liSelected.removeClass('selected');
                next = liSelected.next();
                if(next.length > 0) {
                    liSelected = next.addClass('selected');
                    $('#search').val(liSelected.text());
                }
                else {
                    liSelected = li.eq(0).addClass('selected');
                }
            }
            else {
                liSelected = li.eq(0).addClass('selected');
            }
        }
        else if(e.which === 38) {
            if(liSelected) {
                liSelected.removeClass('selected');
                next = liSelected.prev();

                if(next.length > 0) {
                    liSelected = next.addClass('selected');
                }
                else {
                    liSelected = li.last().addClass('selected');
                }
            }
            else {
                liSelected = li.last().addClass('selected');
            }
        }
        else if(e.which === 13) {
            $('#search').val(liSelected.text());
            $("#dropdown").hide()
            $('#search').blur();
        }
    });
});
  • 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-13T15:56:11+00:00Added an answer on June 13, 2026 at 3:56 pm

    The point is you are showing only elements out of a .filter() call, but the elements are still present in the same list. You need to create another list, hidden, where you will put the hidden elements. Each time the user update the search field, you have to check each element in both lists: the one who needs to be visible will go in the visible list, the ones you don’t want to show will go in the hidden list.

    Here’s an example

    $("input").keyup(function(e) {
        if (this.value.length) {
            var that = this;
            $("#dropdown li").each(function() {
    
                if ( $(this).html().toLowerCase().indexOf(that.value.toLowerCase()) == -1 )
                    $(this).appendTo($hidden);
            });
            $('#hidden li').each(function() {
                if ( $(this).html().toLowerCase().indexOf(that.value.toLowerCase()) !== -1 )
                    $(this).appendTo('#list');
            });
            $("#dropdown").show();
        }
        else {
            $("#dropdown").hide();
        }
        if ( e.which !== 40 && e.which !== 38 )
        {
            $('#dropdown li,#hidden li').each( function() {
                $(this).removeClass( 'selected' );
            });
            liSelected = null;
        }                                            
    });
    

    You now have two lists to check every time. Caching the results of the li’s is not possible now like you were doing before, as their number and position is constantly changing.

    Here I changed your code to show you what I mean, there may be some bugs but you should get the point.

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

Sidebar

Related Questions

I have a master page that contains a search box which is validated as
I have to add a search box, which has dropdown button kind (say it
I have a search box that has a default value as Search,i need to
I have a page that has a search function in a controller which works
I have the following problem: I have a search box in which user can
I have an edit text which functions as a search box in my application.
In my jsp application I have a search box that lets user to search
I have a popup search box that is shown when the user mouses over
I have a simple search box that it hidden until hover(over) and hides again
I have a UI with a search text box and a button that that

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.