Peter Craig
Peter Craig

Reputation: 7289

JavaScript Auto suggest to update multiple fields

I have a simple autocomplete field that spits out a bunch of suggested words and puts it in the suggest field. Is there an auto suggest code that can enter the selected text but also auto fill a range of other fields instead of the selected field?

For example, searching through a contacts list you type their name or address and it suggests options. On clicking one it automatically draws from the database the rest of the available contact details.

Upvotes: 0

Views: 1331

Answers (2)

Josh Matthews
Josh Matthews

Reputation: 13026

If you use Ajax.Autocompleter from script.aculo.us, you can override the afterUpdateElement function to do this for you. You could then make a call that could send back JSON like the following:

{ fields: ['first', 'second'],
  first: 'value',
  second: 'another value' }

and populate your form with something like:

for(field in json.fields)
  $(field).value = json.getAttribute(field);

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532455

You ought to be able to do this with the Autocomplete plugin. Add a result handler and use it to populate your other fields.

 $('div#result').result( function(e,data,formatted) {
      $(this).html(formatted);
      $('div#address').html(data.address);
      ...
 });

Upvotes: 2

Related Questions