Is there a way to make an autocomplete field submit once the option is selected? I know this only works with a single field type scenario. Currently how it works is you select the option from the drop down list and then you have to select the submit button. If doing this with your keyboard, you essentially have to hit "enter" twice. Some users have complained about this.

Any information would be great. Thanks.

Comments

furberd’s picture

I too found it annoying that it's so EASY to get an autocomplete on the page, and yet it doesn't behave like I would expect a single field autocomplete to work.

The following javascript worked for me in Drupal 7:

(function ($) {

$(document).ready(function(){
    $('input.form-autocomplete').change(function(){
        var text = $(this).val($('#autocomplete li.selected div').text());
        if (text.length > 0) {
            var form = $(this).parents('form');
            $(this).addClass('throbbing');
            form.unbind('submit', Drupal.autocompleteSubmit);
            form.submit();
        }
        ;
    });
});

})(jQuery);

Put it in a file and load it with your theme. Note that it will interfere with multiple entry autocompletes, such as tags. If you're using those too, then be more selective about which pages you include the script on. I did it via a form alter on the search form:

function mymodule_form_search_block_form_alter(&$form, &$form_state)
{
	$form['search_block_form']['#autocomplete_path'] = 'search_autocomplete';
	drupal_add_js('path/to/the/js/with/the/code/above/autocomplete.js', 'file');
}