-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (43 loc) · 1.53 KB
/
script.js
File metadata and controls
54 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const browsers = ["Firefox", "Chrome", "Opera", "Safari", "Microsoft Edge",
"Vivaldi", "Brave", "Internet Explorer", "Yandex Browser"];
const input = document.getElementById('browser');
const suggestionsContainer = document.getElementById('suggestions');
function getSuggestions(inputText) {
if(!inputText) return [];
return browsers.filter(browser =>
browser.toLowerCase().startsWith(inputText.toLowerCase())
);
}
function showSuggestions(suggestions) {
suggestionsContainer.innerHTML = '';
if(suggestions.length === 0) {
suggestionsContainer.style.display = 'none';
return;
}
suggestions.forEach(suggestion => {
const div = document.createElement('div');
div.className = 'suggestion-item';
div.textContent = suggestion;
div.addEventListener('click', () => {
input.value = suggestion;
suggestionsContainer.style.display = 'none';
});
suggestionsContainer.appendChild(div);
});
suggestionsContainer.style.display = 'block';
}
input.addEventListener('input', function() {
const inputText = this.value;
const suggestions = getSuggestions(inputText);
showSuggestions(suggestions);
});
document.addEventListener('click', function(e) {
if (e.target !== input) {
suggestionsContainer.style.display = 'none';
}
});
input.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
suggestionsContainer.style.display = 'none';
}
});