Skip to content

Instantly share code, notes, and snippets.

@Cheslab
Last active January 18, 2018 01:19
Show Gist options
  • Save Cheslab/145fff190b1f69b495840c58f7e59f2d to your computer and use it in GitHub Desktop.
Save Cheslab/145fff190b1f69b495840c58f7e59f2d to your computer and use it in GitHub Desktop.
Input autocomplete with ajax
// jQuery UI is required
$("#target_input").autocomplete({
minLength: 2,
source: "ajax/autocomplete.php", // expects JSON
select: function(event, ui) {
$("#target_input").val(ui.item.label); // display the selected text
$("#target_input-id").val(ui.item.value); // save selected id to hidden input
event.preventDefault(); // prevent autocomplete from updating the textbox
return false; // prevent from inserting id into the input instead of label
},
// To prevent inserting ID instead of label when you press the down the arrows:
focus: function(event){
event.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment