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

  • Home
  • SEARCH
  • 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 9030979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:37:22+00:00 2026-06-16T07:37:22+00:00

I am trying to create an auto-suggest box. When a user starts typing in

  • 0

I am trying to create an auto-suggest box. When a user starts typing in the #tagSelection input field, JQuery fetches suggestions from the database through Ajax and display these in the #suggestBox div that is displayed right under the #tagSelection field.

Now I want to enable the user to use the arrow down key to select one of the suggestions. I have started building this by handling the keydown() event and assigning a class to the first entry during this event. The problem I am facing is that the class is removed again when I let go of the arrow down key. I need it to stay assigned when I let go of the key. Is that possible?

The HTML

<div class="form-entry">
    <input id="tagSelection" name="tags" type="text" value="" size="40">

    <div id="suggestBox" style="">
        <a href="#" id="1">design</a>
        <a href="#" id="3">debit card</a>
        <a href="#" id="4">deer</a>
    &nbsp;
    <a href="#" id="addTag">Add word</a>

    <div id="selectedTags"></div>

</div>

The JQuery

(function() {
    $('#tagSelection').keydown(downArrowKeyHandler);
})();

function downArrowKeyHandler(event) {
    if (event.keyCode == 40) {
        if ($('#suggestBox').length > 0) {
            if ($('[tagSelected = 1]').length == 0) {
                // the tagSelected class gives the entry a colored background
                $('#suggestBox').children().filter(':first').addClass('tagSelected');
            }
        }
    }
}
  • 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-16T07:37:23+00:00Added an answer on June 16, 2026 at 7:37 am

    I found the exact same problem here:

    How to make a permanent change to an element after using jQuery's .keydown()?

    It seems I indeed had some other JQuery code that was interfering with my event handler. Whenever someone releases a key when in the #tagSelection field a JQuery method fires that retrieves the suggestions from the db. The already present keyup() event was interfering with my new keydown() event.

    (function() {
        $('#tagSelection').keyup(tagboxTypingHandler)
    })();
    
    
    function tagboxTypingHandler(event) {
        if (event.keyCode != 40) {
            var substring = $('#tagSelection').val();
            if (substring.length > 1) {
                $.get("http://localhost:8080/tag/search/" + substring, returnedTagsHandler);
            }
        }
    }
    

    I just added the event.keyCode != 40 condition and now the class remains on the suggestion and it works 🙂

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

Sidebar

Related Questions

I am trying to create a function that auto focuses an input field when
I am trying to create auto-vectorized code with clang-3.2 From the slides here -
I'm trying to create an auto generated post excerpt from the current page's post
I'm trying to implement a facebook search in my system (auto suggest while typing).
I am trying to create gravatar auto load while user is writing his e-mail
I'm trying to create an auto-complete box and have been having problems due to
I trying to create a auto-complete search script (jQuery + PHP) but I have
I'm trying to create a good match query for an auto-complete search box using
I am trying to create an auto-refresh for a table using ajax/jquery, php/html, and
I'm trying to create an autosuggest search box using jQuery UI and I would

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.