-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch-bar.html
116 lines (100 loc) · 3.07 KB
/
search-bar.html
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Bar</title>
<script src="autocomplete.js"></script>
</head>
<!-- search-bar.html -->
<div id="search-container">
<input type="text" id="search-input" placeholder="Search for a company..." oninput="showSuggestions()" onkeydown="handleKeyDown(event)">
<div id="suggestions" class="suggestions"></div>
</div>
<script>
// Company names for the search autofill
const companyNames = [
"Our Telecom Company",
"Telecom Giant",
"Network Solutions",
"DataConnect",
"Call Unlimited",
"Global Communications",
"Fiber Optics Co"
];
let selectedIndex = -1;
function showSuggestions() {
const input = document.getElementById("search-input").value.toLowerCase();
const suggestionsBox = document.getElementById("suggestions");
suggestionsBox.innerHTML = "";
selectedIndex = -1;
if (input) {
const filteredSuggestions = companyNames.filter(name => name.toLowerCase().includes(input));
filteredSuggestions.forEach((name, index) => {
const suggestionItem = document.createElement("div");
suggestionItem.classList.add("suggestion-item");
suggestionItem.textContent = name;
suggestionItem.onclick = () => selectSuggestion(name);
suggestionsBox.appendChild(suggestionItem);
});
}
}
function selectSuggestion(name) {
document.getElementById("search-input").value = name;
document.getElementById("suggestions").innerHTML = "";
}
function handleKeyDown(event) {
const suggestionsBox = document.getElementById("suggestions");
const items = Array.from(suggestionsBox.getElementsByClassName("suggestion-item"));
if (event.key === "ArrowDown") {
selectedIndex = (selectedIndex + 1) % items.length;
updateSelection(items);
} else if (event.key === "ArrowUp") {
selectedIndex = (selectedIndex - 1 + items.length) % items.length;
updateSelection(items);
} else if (event.key === "Enter") {
if (selectedIndex > -1) {
selectSuggestion(items[selectedIndex].textContent);
selectedIndex = -1;
event.preventDefault();
}
}
}
function updateSelection(items) {
items.forEach((item, index) => {
item.classList.toggle("selected", index === selectedIndex);
});
}
</script>
<style>
#search-container {
position: relative;
width: 300px;
}
#search-input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.suggestions {
position: absolute;
top: 100%;
left: 0;
right: 0;
border: 1px solid #ddd;
border-top: none;
max-height: 150px;
overflow-y: auto;
background-color: white;
z-index: 1000;
}
.suggestion-item {
padding: 8px;
cursor: pointer;
}
.suggestion-item:hover, .suggestion-item.selected {
background-color: #f0f0f0;
}
</style>
</body>
</html>