I have a script which allows to display Google suggestions. The problem is that it STILL displays the list even when the value of input is different than the data available, I rather have it closed. Take a look at this Fiddle: JsFiddle
Type a letter than some rubbish like ‘cjfsdkcrdvhcxrbsks’ You will see the list is still showing. So how do I close it if the value does not match the suggestions?
You actually got an error if you have a look in the console.
When the search does not return results, the object
data.query.results.toplevelis null. As you don’t check for that, there is an exception when trying to accessdata.query.results.toplevel.CompleteSuggestionand the menu does not close simply because the code just stops working.Check for null for
data.query.results.topleveland in case it is null, useresponse([]);to close the menu:DEMO