I am using autocomplete api of jquery in order to fetch the name from the database.But i want to show an alert message on selecting a name from the textbox displayed.I will show
If you're using devbridge's Ajax Autocomplete for jQuery:
Use a settings object to set the instance up, with your URL and an event handler on selecting a value:
$(document).ready(function() {
var selectedValue = '';
$( '#country' ).autocomplete({
serviceUrl: 'list.jsp',
onSelect: function(suggestion) {
selectedValue = suggestion.value;
alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
});
If you're using jQuery UI's Autocomplete:
Handle the autocompleteselect
event rather than the change
event and access the value of the selected item by using ui.item.value
rather than .val()
, where ui
is the second argument provided in the event handler function.
$(document).ready(function() {
var selectedValue = '';
$( '#country' ).autocomplete({
source: 'list.jsp',
select: function(e, ui) {
selectedValue = ui.item.value;
alert(selectedValue);
}
});
});
See https://api.jqueryui.com/autocomplete/#event-select