Tuesday, December 30, 2008

[jQuery] jquery autocomplete plugin

Hi list, I need some help with autocomplete plugin.

I have two text field (name and surname) and I need to add to the name
field the autocomplete like this:
When the user start writing on it, it shows the coincidences and when
he select the correct item the surname field most be filled too. For
example:

HTML tags:

<input type="text" id="name" /><br />
<input type="text" id="surname" /><br />

Javascript code:

$().ready(function() {
$("#name").autocomplete(records, {
minChars: 0,
width: 250,
matchContains: 1,
autoFill: true,
onItemSelect: function(row) {
$("#surname").value = row.surname;
},
onFindValue: function(row) {
$("#surname").value = row.surname;
},
formatItem: function(row, i, max) {
return row.name + " " + row.surname;
},
formatMatch: function(row, i, max) {
return row.name;
},
formatResult: function(row) {
return row.name;
}
});
});

*************************
records its a javascript array like:

var records = [
{ name: "John", surname: "Smith"},
{ name: "Ian", surname: "Crawford"},
{ name: "Derek", surname: "Lee"}
];

Thanks!!!

No comments: