Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://kraaden.github.io/autocomplete/autocomplete.js"></script>
  <link rel="stylesheet" type="text/css" href="https://kraaden.github.io/autocomplete/autocomplete.css">
</head>
<body>
  <p>Try to click on different words using your mouse:</p>
  <input id="country" type="text" value="Aust Ger" />
</body>
</html>
 
var countries = [
    { label: 'Germany', value: 'DE' },
    { label: 'Austria', value: 'AU' }
];
var input = document.getElementById("country");
function getWord(s, pos) {
    const n = s.substring(pos).match(/^[a-zA-Z0-9-_]+/)
    const p = s.substring(0, pos).match(/[a-zA-Z0-9-_]+$/)
    if (!p && !n) return ''
    return (p || '') + (n || '')
}
autocomplete({
    minLength: 0,
    input: input,
    fetch: function(text, update) {
        text = getWord(text, input.selectionStart).toLowerCase();
        var suggestions = countries.filter(n => n.label.toLowerCase().startsWith(text))
        update(suggestions);
    },
    onSelect: function(item) {
        input.value = item.label;
    },
    click: function(e) { e.fetch(); }
});
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers