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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:14:52+00:00 2026-06-01T03:14:52+00:00

I do not have enough knowledge in JQuery to figure out, where in this

  • 0

I do not have enough knowledge in JQuery to figure out, where in this code below i’d mistaken. I’m not sure about .live handler. Anyway, this code stops on first <li> with added class ‘selected‘ on keydown event. I want it to check all <li> on each keydown.
Thank you for any constructive comments.

  • suggest – input field, on keyup appears autosuggest list,
  • result – <ul id='result'></ul>
  • selected – <li>‘s

Script:

$('#suggest').live('keyup keydown', function(event) {
    var search = $('#suggest').val();
    $.post('search.php', {
        search: search
    }, function(data) {
        $('#dropdown').html(data);

        switch (event.which) {
        case 40:

            var found = 0;

            $('#result li').each(function() {
                if ($(this).attr("class") == "selected") {
                    found = 1;
                }
            });

            if (found == 1) {
                var sel = $("#result li[class='selected'");

                // check if this is a last element in the list
                // if so then add selected class to the first element in the list
                if (sel.next().text() == '') {
                    $("#result li:first").addClass("selected");
                } else {
                    sel.next().addClass('selected');
                    // remove class selected from previous item
                    sel.removeClass('selected');
                }
            } else {
                $("#result li:first").addClass("selected");
            }
            break;

        case 38:
            //bla-bla
            break;
        }
    });
});
  • 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-01T03:14:53+00:00Added an answer on June 1, 2026 at 3:14 am

    A few notes:

    1. To check if an element has a class, never use .attr("class") ==. If the element has more than one class, or the attribute is poorly formatted, this will never match. jQuery provides .hasClass() for just this purpose. $(this).hasClass('selected')
    2. You don’t need to loop through each element with .each() to see if one has a class. You can call .hasClass() on the whole group.
    3. Likewise to select an element with a class, use .myclass not [class="myclass"]. This is for similar reasons as point #1. var sel = $("#result li.selected");
    4. To see if an element is the last sibling, don’t use sel.next().text() == ''. This will end up calling text() on an undefined object (because there is none). Instead use .length to see if there is/isn’t a next object: sel.next().length == 0
    5. You probably don’t need to search when the up/down keys are pressed. I would put an if/then statement to do the up/down stuff separate from searching.
    6. Your up and down code will be very similar. Instead of using a case for each key, use the same code for both, but modify it slightly depending on whether you have up or down.
    7. Using keyup and keydown will cause your up/down keys to fire twice, meaning your user will only be able to step 2 or more items at a time. Either just use one, or only respond to one for the up/down code using if(event.type == 'keydown') {

    With all of that, everything seems to work ok:

    http://jsfiddle.net/wgQE5/2/

    Is there some other behavior you were wanting?

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

Sidebar

Related Questions

I know that given enough time I'll figure this out, but it sure would
This probably has a simple answer, but I must not have had enough coffee
Greetings, Apologies in advance that I have not researched this toughly enough to answer
Ok i just don't have enough knowledge for this simple script apparently. Basically, in
First off, I don't have any knowledge about java/jquery/ajax or such, but I think
I don't have enough C knowledge to work out the right thing to do
I have not used Twitter enough to become familiar with its terminology or the
You're kidding, right? Not only is it bad enough that I have to select
VB 6.0 does not have any global handler.To catch runtime errors,we need to add
Note: To answer this question, you shouldn't have to know anything about Selenium or

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.