auto-complete plug-in patch for custom callback to auto-populate form based on selected results

auto-complete plug-in patch for custom callback to auto-populate form based on selected results

In regards to the 'autocomplete' plugin available from Jörn Zaefferer. I needed some added functionality to query a server after a user selected an item from your autopopulate list to then populate an existing form. Here is my solution...

Added functionality of a callBack option Add the following three lines on or near line 235

  1. $input.trigger("result", [selected.data, selected.value]);
  2. +if(options.callBack){
    + options.callBack(selected.value);
    +}

Now just add your callBack option with your custom function like so:

  1. $j("#autoOrders").autocomplete(orders, {
      callBack: queryOrders
    });

Of course your custom callBack function may be different but here is an example of formating a query string then calls a secondary helper function to auto populate the form.

  1. function queryOrders(data)
    {
     var x = data.split(" ");
     var y = '?date='+x[0]+'&number='+x[1]+'&account='+x[2]+'&eta='+x[3];
     return mySearches(y);
    }

    function mySearches(data)
    {
     $j.ajax({
      data: data,
      type: 'post',
      url: 'ajaxSearch.php',
      success: function(response) {
       var myObj = JSON.parse(response, function(key, value) {
       $j('#'+key).val(value);
      });
     }
    });

















Hope this helps someone else.

Just in case I didn't explain it right.

By adding an option for a post selected autocomplete function you can auto-populate a form based on your own custom callback.