I looked for an effective solution regarding your request but wasn't able to find anything working through Google ( except Google itself!… =) ).
I tried to do things with jQuery and jQuery-UI… and in the end, I started over from the beginning.
Anyway, here is what I've read and want to share with you, I'm sure you'll find something interesting in all this.
About Google
Here is some answers to how google instant works:
How does Google Instant work?
https://searchengineland.com/how-google-instant-autocomplete-suggestions-work-62592
Also, Google isn't using only autocomplete, but a prediction algorithm too: https://support.google.com/websearch/answer/106230?hl=en
https://blog.google/products/search/google-search-autocomplete/
Visually, we could try to do something similar to google, and this link could be helpfull:
How to implement a google suggest-like input field?
What I'll do
Here is the snippet I ended with:
$(function() {
var states = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia',
'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa',
'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina',
'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee',
'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming'
];
$("#tags").autocomplete({
source: states,
autoSelectFirst: true,
autoFocus: true
});
});
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
As we're not Google, we can't predict what the user wants to write.
So, in my snippet, they need to press enter or tab to confirm the autocomplete. It is not wise to auto-select the first hit after the user typed only two or three characters.
But, if we do auto-select anyway… Which one will be auto-selected ?
For example, when I am starting to type “New ”, the auto-selection will certainly be “New York” if we follow Google suggestion, “New Hampshire” if we're based on the alphabetical order, and it could be “New Mexico” if based on the user location.
Hope it helps, in any way.