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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:24:25+00:00 2026-06-03T18:24:25+00:00

I am using an autocomplete on my search box. Everything is working properly, except

  • 0

I am using an autocomplete on my search box. Everything is working properly, except that the mouse click event doesn’t work if you use the mouse to select suggestion instead of using up or down arrows. On rare occasions when the page is first loaded it will work once but never again. Not sure what I’m doing wrong.

Test page is located at http://www.candyundies.com/template_non_product.php

Here is the contents of autocomplete.js:

// global variables
var acListTotal   =  0;
var acListCurrent = -1;
var acDelay       = 100;
var acURL         = null;
var acSearchId    = null;
var acResultsId   = null;
var acSearchField = null;
var acResultsDiv  = null;
function setAutoComplete(field_id, results_id, get_url) {
// initialize vars
acSearchId  = "#" + field_id;
acResultsId = "#" + results_id;
acURL       = get_url;
// create the results div
$("#auto").append('<div id="' + results_id + '"></div>');
// register mostly used vars
acSearchField   = $(acSearchId);
acResultsDiv    = $(acResultsId);
// on blur listener
acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 100) });
// on key up listener
acSearchField.keyup(function (e) {
    // get keyCode (window.event is for IE)
    var keyCode = e.keyCode || window.event.keyCode;
    var lastVal = acSearchField.val();
    // check an treat up and down arrows
    if(updownArrow(keyCode)){
        return;
    }
    // check for an ENTER or ESC
    if(keyCode == 13 || keyCode == 27){
        clearAutoComplete();
        return;
    }
    // if is text, call with delay
    setTimeout(function () {autoComplete(lastVal)}, acDelay);
});
}
// treat the auto-complete action (delayed function)
function autoComplete(lastValue) {
// get the field value
var part = acSearchField.val();
// if it's empty clear the resuts box and return
if(part == ''){
    clearAutoComplete();
    return;
}
// if it's equal the value from the time of the call, allow
if(lastValue != part){
    return;
}
// get remote data as JSON
$.getJSON(acURL + part, function(json){
    // get the total of results
    var ansLength = acListTotal = json.length;
    // if there are results populate the results div
    if(ansLength > 0){
        var newData = '';
        // create a div for each result
        for(i=0; i < ansLength; i++) {
            newData += '<div class="unselected">' + json[i] + '</div>';
        }
        // update the results div
        acResultsDiv.html(newData);
        acResultsDiv.css("display","block");
        // for all divs in results
        var divs = $(acResultsId + " > div");
        // on mouse over clean previous selected and set a new one
        divs.mouseover( function() {
            divs.each(function(){ this.className = "unselected"; });
            this.className = "selected";
        });
        // on click copy the result text to the search field and hide
        divs.click( function() {
            acSearchField.val(this.childNodes[0].nodeValue);
            clearAutoComplete();
        });
    } else {
        clearAutoComplete();
    }
});
}
// clear auto complete box
function clearAutoComplete() {
acResultsDiv.html('');
acResultsDiv.css("display","none");
}
// treat up and down key strokes defining the next selected element
function updownArrow(keyCode) {
if(keyCode == 40 || keyCode == 38){
    if(keyCode == 38){ // keyUp
        if(acListCurrent == 0 || acListCurrent == -1){
            acListCurrent = acListTotal-1;
        }else{
            acListCurrent--;
        }
    } else { // keyDown
        if(acListCurrent == acListTotal-1){
            acListCurrent = 0;
        }else {
            acListCurrent++;
        }
    }
    // loop through each result div applying the correct style
    acResultsDiv.children().each(function(i){
        if(i == acListCurrent){
            acSearchField.val(this.childNodes[0].nodeValue);
            this.className = "selected";
        } else {
            this.className = "unselected";
        }
    });
    return true;
} else {
    // reset
    acListCurrent = -1;
    return false;
}
}
  • 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-03T18:24:27+00:00Added an answer on June 3, 2026 at 6:24 pm

    isn’t divs an array?

    you can’t override a UI event for an array, it doesn’t exist. Unless i’m syntactually missing something here.

    Try iterating and adding the event

     for( var i 0; i < divs.length;i++)
           divs[i].onclick = ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using 'rails3-jquery-autocomplete' gem, but it doesn't have multi column search, but there is
I am using the jQueryUI autocomplete on a search box. All works fine. However,
I am using jQuery.autocomplete(1.02) on my search box and I want exact string and
I'm using jQuery AutoComplete, and I have an auto-complete search box to search for
I have a search box in my application that uses the jQuery UI Autocomplete
I am using AutoComplete widget. It works fine for two characters search but doesen't
Note: In this question I'm using the term autocomplete (or iterative search) to refer
I have a project that require me to do a auto complete search using
I am using jquery autocomplete pluguin. Configured to use multiple Values. I set option
Im using JQuerys Autocomplete plugin, but it doesn't autocomplete upon entering anything. Any ideas

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.