Youss
Youss

Reputation: 4212

Disable and enable Jquery autocomplete

The Jquery autocomplete (LINK) has an option to turn it off, which looks like this:

$(input).autocomplete({ disabled: true });

I would like it to be turned off because its default settings is to respond on keyup. I much rather have it function on keydown. So I turned it off and I wrote a function with an event keydown like this:

timer = 0;
function func (){ 

var val = $(input).val();
$(input).autocomplete('search', val);
}


$(input).live('keydown', function(){

    if (timer) {
        clearTimeout(timer);
    }

    timer = setTimeout(func, 50000); 
     $(input).autocomplete( "enable" );
});

It doesn't work...meaning it does not do search after 50000 ms but instead it does the default setting with keyup. What should I change?

Upvotes: 3

Views: 7407

Answers (2)

lucuma
lucuma

Reputation: 18339

Add a keyup event and stop the event propogation.

Add a keydown event and call it like this: $("input").autocomplete('search', 'demo-value');

$(input).autocomplete().keyup(function(event) {
  event.stopPropagation();
}).keydown(function() {
$(this).autocomplete('search', $(input).val());  //this or $(input)...
});

Upvotes: 1

Farhan Ahmad
Farhan Ahmad

Reputation: 5198

$( "#autocomplete" )
      .keydown( function( event ) {
            var isOpen = $( this ).autocomplete( "widget" ).is( ":visible" );
            var keyCode = $.ui.keyCode;
            if ( !isOpen && ( event.keyCode == keyCode.UP || event.keyCode == keyCode.DOWN ) ) {
                  event.stopImmediatePropagation();
            }

      })
      .autocomplete({
            source: someSource
      });

Upvotes: 0

Related Questions