Skip to content

Commit

Permalink
Autocomplete: Handle clicks outside the autocomplete after scrolling …
Browse files Browse the repository at this point in the history
…the results (which causes the body to gain focus). Fixes #5903 - Autocomplete doesn't close after scrolling.
  • Loading branch information
scottgonzalez committed Aug 5, 2010
1 parent ed07f0a commit de266a1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ui/jquery.ui.autocomplete.js
Expand Up @@ -119,7 +119,24 @@ $.widget( "ui.autocomplete", {
.addClass( "ui-autocomplete" )
.appendTo( $( this.options.appendTo || "body", doc )[0] )
// prevent the close-on-blur in case of a "slow" click on the menu (long mousedown)
.mousedown(function() {
.mousedown(function( event ) {
// clicking on the scrollbar causes focus to shift to the body
// but we can't detect a mouseup or a click immediately afterward
// so we have to track the next mousedown and close the menu if
// the user clicks somewhere outside of the autocomplete
var menuElement = self.menu.element[ 0 ];
if ( event.target === menuElement ) {
setTimeout(function() {
$( document ).one( 'mousedown', function( event ) {
if ( event.target !== self.element[ 0 ] &&
event.target !== menuElement &&
!$.ui.contains( menuElement, event.target ) ) {
self.close();
}
});
}, 1 );
}

// use another timeout to make sure the blur-event-handler on the input was already triggered
setTimeout(function() {
clearTimeout( self.closing );
Expand Down

1 comment on commit de266a1

@jzaefferer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What an endless source of joy!

Please sign in to comment.