Jquery autocomplete passing additional parameters

Sometimes you may need to use jquery autocomplete function passing additional parameters. I had a such requirement  in one of my recent project. I searched here and there to find the right solution and it was quite hard. So, I thought to share what I did to get it success here.

Why additional parameters should pass to autocomplete?

Lets think of a situation where searching a certain term in a field of database table. You may need to use extra condition which relevant with a client side variable like input value of a form. In such case, that particular input value is to be passed to the script processing search functionality in addition to the search term. This is where you need a functionality to pass additional parameter to jquery autocomplete function.

This is the syntax for pass additional parameters to jquery autocomplete.

$("selector").autocomplete({
    source: function(request, response) {
        $.getJSON(url, { key1:value1, key2:value2 }, response);
    },
    minLength: 2
 });

If this syntax is used, the search term should pass as one parameter. And any number of additional parameters can be passed. Here is one practical example where I passed additional parameters to jquery autocomplete.

$("#account-number").autocomplete({
    source: function(request, response) {
        $.getJSON(SITE_NAME + '/bankaccount/getaccounts/', { bank_code: $('#bank-code').val(), term : request.term }, response);
    },
    minLength: 2
});

Think you found something useful reading this article. Thank you for reading