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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:15:09+00:00 2026-05-19T09:15:09+00:00

Before you point me to them, yes, I have reviewed the half dozen posts

  • 0

Before you point me to them, yes, I have reviewed the half dozen posts on this topic, but I am still stymied as to why this doesn’t work.

My goal is to detect when the autocomplete yields 0 results. Here’s the code:

 $.ajax({
   url:'sample_list.foo2',
   type: 'get',
   success: function(data, textStatus, XMLHttpRequest) {
      var suggestions=data.split(",");

  $("#entitySearch").autocomplete({ 
    source: suggestions,
    minLength: 3,
    select: function(e, ui) {  
     entityAdd(ui.item.value);
     },
    open: function(e, ui) { 
     console.log($(".ui-autocomplete li").size());
     },
    search: function(e,ui) {
     console.log("search returned: " + $(".ui-autocomplete li").size());

    },
    close: function(e,ui) {  
     console.log("on close" +  $(".ui-autocomplete li").size());    
     $("#entitySearch").val("");
    }
   }); 

  $("#entitySearch").autocomplete("result", function(event, data) {

   if (!data) { alert('nothing found!'); }

  })
 }
}); 

The search itself works fine, I can get results to appear without a problem. As I understand it, I should be able to intercept the results with the autocomplete(“result”) handler. In this case, it never fires at all. (Even a generic alert or console.log that doesn’t reference the number of results never fires). The open event handler shows the correct number of results (when there are results), and the search and close event handlers report a result size that is always one step behind.

I feel like I’m missing something obvious and glaring here but I just don’t see it.

  • 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-19T09:15:10+00:00Added an answer on May 19, 2026 at 9:15 am

    jQueryUI 1.9

    jQueryUI 1.9 has blessed the autocomplete widget with the response event, which we can leverage to detect if no results were returned:

    Triggered after a search completes, before the menu is shown. Useful for local manipulation of suggestion data, where a custom source option callback is not required. This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled.

    So, with that in mind, the hacking we had to do in jQueryUI 1.8 is replaced with:

    $(function() {
        $("input").autocomplete({
            source: /* */,
            response: function(event, ui) {
                // ui.content is the array that's about to be sent to the response callback.
                if (ui.content.length === 0) {
                    $("#empty-message").text("No results found");
                } else {
                    $("#empty-message").empty();
                }
            }
        });
    });​
    

    Example: http://jsfiddle.net/andrewwhitaker/x5q6Q/


    jQueryUI 1.8

    I couldn’t find a straightforward way to do this with the jQueryUI API, however, you could replace the autocomplete._response function with your own, and then call the default jQueryUI function (updated to extend the autocomplete’s prototype object):

    var __response = $.ui.autocomplete.prototype._response;
    $.ui.autocomplete.prototype._response = function(content) {
        __response.apply(this, [content]);
        this.element.trigger("autocompletesearchcomplete", [content]);
    };
    

    And then bind an event handler to the autocompletesearchcomplete event (contents is the result of the search, an array):

    $("input").bind("autocompletesearchcomplete", function(event, contents) {
        $("#results").html(contents.length);
    });
    

    What’s going on here is that you’re saving autocomplete’s response function to a variable (__response) and then using apply to call it again. I can’t imagine any ill-effects from this method since you’re calling the default method. Since we’re modifying the object’s prototype, this will work for all autocomplete widgets.

    Here’s a working example: http://jsfiddle.net/andrewwhitaker/VEhyV/

    My example uses a local array as a data source, but I don’t think that should matter.


    Update: You could also wrap the new functionality in its own widget, extending the default autocomplete functionality:

    $.widget("ui.customautocomplete", $.extend({}, $.ui.autocomplete.prototype, {
    
      _response: function(contents){
          $.ui.autocomplete.prototype._response.apply(this, arguments);
          $(this.element).trigger("autocompletesearchcomplete", [contents]);
      }
    }));
    

    Changing your call from .autocomplete({...}); to:

    $("input").customautocomplete({..});
    

    And then bind to the custom autocompletesearchcomplete event later:

    $("input").bind("autocompletesearchcomplete", function(event, contents) {
        $("#results").html(contents.length);
    });
    

    See an example here: http://jsfiddle.net/andrewwhitaker/VBTGJ/


    Since this question/answer has gotten some attention, I thought I’d update this answer with yet another way to accomplish this. This method is most useful when you have only one autocomplete widget on the page. This way of doing it can be applied to an autocomplete widget that uses a remote or local source:

    var src = [...];
    
    $("#auto").autocomplete({
        source: function (request, response) {
            var results = $.ui.autocomplete.filter(src, request.term);
    
            if (!results.length) {
                $("#no-results").text("No results found!");
            } else {
                $("#no-results").empty();
            }
    
            response(results);
        }
    });
    

    Inside the if is where you would place your custom logic to execute when no results are detected.

    Example: http://jsfiddle.net/qz29K/

    If you are using a remote data source, say something like this:

    $("#auto").autocomplete({
        source: "my_remote_src"
    });
    

    Then you’ll need to change your code so that you make the AJAX call yourself and can detect when 0 results come back:

    $("#auto").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "my_remote_src", 
                data: request,
                success: function (data) {
                    response(data);
                    if (data.length === 0) {
                        // Do logic for empty result.
                    }
                },
                error: function () {
                    response([]);
                }
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has been asked before but I have looked at every answer
Before I embark on writing my own solution to this issue, can anyone point
Before you answer this I have never developed anything popular enough to attain high
Before I start, I know there is this post and it doesn't answer my
Before you start firing at me, I'm NOT looking to do this, but someone
This is a problem I have run into before and I have yet to
Before you jump to conclusions, yes, this is programming related. It covers a situation
Before I do this I figured I would ask if it was the best
Before, I have found the Cost in the execution plan to be a good
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of

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.