I am using this jquery autocomplete code:
$(document).ready(function(){
var data = <?php echo $data; ?>;
$("#contact_name").autocomplete({
source:data,
select: function(e, ui) {
e.preventDefault() // <--- Prevent the value from being inserted.
$("#contact_email").val(ui.item.value);
$(this).val(ui.item.label);
$("#AddAsContact").css('display', 'none');
$('#AddAsContact').prop('checked', false);
}
});
});
its working great, exactly as it should but if an item from the auto complete list is not selected and something else is just typed into the text input, how can i run some different code. Kind of like else { ...code here... }
select
event is triggered when you select
something from the DropDown.
You can use the change
event for determining whether the item falls under the autocomplete list or not,
change: function (event, ui) {
if (ui.item === null) {
$(this).val('');
$('#field_id').val('');
}
}
And, here is a Demo Fiddle for you.
Hope this help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With