Mohamed Khamis
Mohamed Khamis

Reputation: 8029

Setting JQuery's Autocomplete to suggest only on hitting enter

I am trying to adjust the autocompletion to show me the suggestions only when I hit the enter button, not as I type.. How is this possible? I think it would be easy to edit the code in the JQuery file itself, but I am trying to avoid that, is there a way to alter functions or so? At this point, I'll need to make an Ajax call to get the data that shall be used as the 'source' of the autocomplation box, so I will need to change the source according to the response..

Upvotes: 0

Views: 668

Answers (2)

Mohamed Khamis
Mohamed Khamis

Reputation: 8029

To sum up, here's how it worked, thanks for Tommy and Jags first disable the the autocomplate in the initialization, by adding this option: disabled: true
Then add this listener:

$('.selector').keyup(function(e){
    if(e.keyCode == 13){ 
        $(this).autocomplete( "enable" ).autocomplete( "search" );
        $(this).autocomplete( "disable" );  
        $('.ui-autocomplete').removeClass('ui-state-disabled');
        $('.ui-autocomplete').css('cursor','pointer');
    }
});

Upvotes: 0

TommyBs
TommyBs

Reputation: 9646

You can set disabled = true when you assign the plugin

$( ".selector" ).autocomplete({ disabled: true });

and then enable on keypress I believe

$( ".selector" ).keypress(function(e){
 if(e.keyCode == 13){ // can't remember exact syntax or keycode here
   $( ".selector" ).autocomplete( "enable" );
 }
});

more info is available on the options and methods tab on the ui help page http://jqueryui.com/demos/autocomplete/#method-option

Upvotes: 1

Related Questions