Additional Parameter when using jqueryui's auto complete

2.1k views Asked by At

I'm looking to add an additional parameter to a jquery UI auto complete request without having to nest the json return in an ajax call. I would envision something like the following working, however the data: option is not passed over to the ajax request like it is on a normal jquery ajax request.

 $("#div").autocomplete({
        source: 'ajax.php',
        minLength: 2,
        data: '&action=getUserName',
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

tl;dr I need help passing &action=getUserName over to the ajax for my autocomplete, preferably without nesting it in an ajax callback.

1

There are 1 answers

6
Chris Baker On

Try this instead:

$("#div").autocomplete({
        source: 'ajax.php',

        extraParams: {
            action: function() {
                  return "getUserName";
            }
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

per docs: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

edit: added correction based on jquery autocomplete extraParams