Last active
January 18, 2018 01:19
-
-
Save Cheslab/145fff190b1f69b495840c58f7e59f2d to your computer and use it in GitHub Desktop.
Input autocomplete with ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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