I have a jquery’s autocomplete texbox and, upon a click of a button, I need to check if the value entered has come from the autocomplete or is it a completely new value.
The problems is that my code constantly gives me ‘alert(“no”)’… maybe I shouldn’t be checking against ‘cache’, but something else?
//////////////////////////////////////////// autocomplete code //////////////////
var cache = {},
lastXhr;
$( "#inputV" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
cache[ term ] = data;
if ( xhr === lastXhr ) {
response( data );
}
});
}
});
////////////////////////// check if input comes from autocomplete //////////////////
$('#btn_check').click(function() {
var input = $("#inputV").val();
alert(input);
if ( input in cache )
{
alert("yes");
}
else
{
alert("no");
}
});

This is how I make it on a project of mine
Where the
fill()function is attached to returned results from the autocomplate. So if user clicks on an item from the list, the fill function will set the value in my html element with id “id” using this$("#id").val(thisID);Simply if user doesn’t use the suggest from the list, the “id” will not be filled. In your case you can just add an hidden input with name/id “isacutocomplate”, attach
fill();function to results returned from the autocomlate and if item is clicked, you will update the “isacutocomplate”, other way you will not fill it. So, when user click your button, you check the “isacutocomplate” input value and you see if it’s from autocomplate’s list or not.Hope that make sense to you. If not, I can provide entire process.