Comments (2)
You can easily detect when the autocomplete is attached by using MutationObserver
as shown below:
HTML:
<!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>
<input id="country" type="text" />
</body>
</html>
Javascript:
var countries = [
{ label: 'United Kingdom', value: 'UK' },
{ label: 'United States', value: 'US' }
];
var input = document.getElementById("country");
autocomplete({
input: input,
fetch: function(text, update) {
text = text.toLowerCase();
var suggestions = countries.filter(n => n.label.toLowerCase().startsWith(text))
update(suggestions);
},
onSelect: function(item) {
input.value = item.label;
}
});
// sample observer code to detect changes in HTML
var targetNode = document.querySelector('body');
var config = { attributes: false, childList: true, subtree: true };
var callback = function(mutationsList, observer) {
for(var mutation of mutationsList) {
for (var node of mutation.addedNodes) {
if (node.classList && node.classList.contains('autocomplete')) {
console.log('autocomplete added');
}
}
for (var node of mutation.removedNodes) {
if (node.classList && node.classList.contains('autocomplete')) {
console.log('autocomplete removed');
}
}
}
};
var observer = new MutationObserver(callback);
observer.observe(targetNode, config);
https://jsbin.com/cebinukuyo/1/edit?html,js,output
from autocomplete.
Perfect, thank you!
from autocomplete.
Related Issues (20)
- iOS candidate word selection does not trigger autocomplete HOT 3
- Container width is always set as the input width causing UI problems HOT 2
- emptyMsg Shows on Resize When Using Custom Container HOT 3
- autocomplete selection is broken on Firefox for Android HOT 2
- add loading state class to input
- Slow navigation with arrow up/down keys due to redraw of autocomplete div HOT 4
- AJAX example HOT 4
- exports missing autocomplete.css HOT 2
- Is this mantained? HOT 1
- What about multiple selection?
- Allow closing the list by pressing Esc HOT 2
- Recall fetch() after user moves the caret HOT 9
- My custom container deleted HOT 4
- use for textarea
- Requesting feature: changing input.value when moving up-down through the suggestions list by keyboard HOT 1
- Prevent that scrolling page fires heavy updates of max-height HOT 1
- Disable rendering on keyup HOT 1
- onBlur prop HOT 3
- Results disappears on keyboard dismissal on iOS HOT 5
- fetch called after autocomplete selection on Firefox Android HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autocomplete.