I'm using autocomplete on one input field to fill in another:
<input id='fruit' name='fruit' type='text'>
<input id='details' name='details' type='text'>
jQuery code:
var x = ["apple", "kiwi", "lemon"];
$( "#fruit" ).autocomplete({
source: x
});
jQuery(document).on("change","#fruit", function()
{
$.each(x, function(key, value) {
switch(value) {
case "apple":
$('#details').val("Delicious");
break;
case "kiwi":
$('#details').val("Yummy");
break;
case "lemon":
$('#details').val("Sour");
}
});
});
When someone selects apple, kiwi or lemon with the autocomplete then it fills out the other input field with the corresponding text.
I have two issues:
- Currently it always prints out "Sour"
- When you select an entry from the autocomplete you have to click away again to fill out the field. Is there a way to do this when someone selects a choice from autocomplete?
Here is a fiddle