Luggest is a lightweight, dependency-free autocomplete library for plain JavaScript.
- No jQuery
- Simple API
- Works with static arrays or remote JSON sources
- Per-element instances (similar to CKEditor / TinyMCE style access)
- 🔎 Autocomplete for any text
<input> - 📃 Static array source or AJAX URL
- 🎯 Supports
{ value, label, metadata }objects or simple string arrays - 🎚 Configurable minimum input length (
min_length, default1, can be0) - 🎛 Events:
on_open,on_select - 🧹 Instance methods:
close(),destroy() - 🧱 Global access via
Luggest.get(id)andLuggest.instances[id]
<script src="src/luggest.js"></script>Just copy src/luggest.js into your project and include it in your layout.
HTML:
<input type="text" id="city-input" autocomplete="off">JavaScript:
<script src="src/luggest.js"></script>
<script>
const cities = ['Prague', 'Brno', 'Ostrava', 'Plzeň', 'Liberec'];
Luggest.init('#city-input', {
source: cities,
min_length: 1,
on_select: function (element, item) {
console.log('Selected:', item);
}
});
</script>Luggest.init(target, options)
-
target:- CSS selector string (e.g.
'#my-input') or - DOM element (
document.getElementById(...)/querySelector(...))
The element must have anidor Luggest will log an error and skip init.
- CSS selector string (e.g.
-
options(all optional):-
sourceArrayorString(URL)- Array mode:
['Prague', 'Brno']- or
[{ value: 'prg', label: 'Prague', metadata: {...} }, ...] - Strings are normalized to
{ value, label }wherevalue === label
- URL mode:
source: '/autocomplete/cities'- Luggest calls:
/autocomplete/cities?term=YOUR_QUERY - The endpoint must return JSON array in the same formats as above.
-
min_length- Minimal number of characters before suggestions are requested.
- Default:
1 - Can be
0. In that case:- On focus, all suggestions are shown (for the current source).
-
max_results- Maximum number of items shown in dropdown.
- Default:
20
-
on_open(element, items)- Called when suggestions are shown.
element– input elementitems– array of normalized{ value, label, metadata }
-
on_select(element, item)- Called when user selects an item (click or Enter).
element– input elementitem– selected item{ value, label, metadata }
-
const instance = Luggest.init('#my-input', {
source: myData,
min_length: 2
});If an instance already exists for that element ID, the existing one is returned.
const inst = Luggest.get('my-input');
// or
const inst2 = Luggest.instances['my-input'];inst.close(); // Close dropdown
inst.destroy(); // Remove dropdown, listeners, and unregister instanceDestroying will also:
- Remove the dropdown container from DOM
- Remove event listeners
- Delete
Luggest.instances[id] - Clear
data-luggeston the element
- Modern evergreen browsers (Chrome, Firefox, Edge, Safari)
- No external dependencies
MIT