1

I'm trying to write a script that needs to know whether or not the autocomplete dropdown contains any results. This is what I currently have, but for some reason the IF is not working (ie. the code inside is not being triggered).

if($("ul.ui-autocomplete li").length == 0){
    $('div#autocomplete').replaceWith('<h2>' + $(this).val() + '</h2>');
    return false;
}

It seems like there are still results, they're just hidden. So how can I see if there are results or if the dropdown autocomplete list is visible?

Adam
  • 9,189
  • 15
  • 46
  • 62
  • 1
    possible duplicate of [Detecting no results on jQuery UI autocomplete](http://stackoverflow.com/questions/4718968/detecting-no-results-on-jquery-ui-autocomplete) – Andrew Whitaker Feb 20 '12 at 22:06

1 Answers1

-2
//in the open event:
open: function(event,ui){
    var len = $('.ui-autocomplete > li').length;
    console.log(len < 1);
}
Johan
  • 35,120
  • 54
  • 178
  • 293
  • 1
    -1: `open` does not get called when there are no results: http://jsfiddle.net/VyAhU/. In fact, just reading the docs for autocomplete would have revealed this. – Andrew Whitaker Feb 20 '12 at 22:10