-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (36 loc) 路 1.58 KB
/
Copy pathscript.js
File metadata and controls
43 lines (36 loc) 路 1.58 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
// Sample fruit list
const fruits = ["Apple 馃崕", "Banana 馃崒", "Grape 馃崌", "Orange 馃崐", "Pear 馃崘", "Pineapple 馃崓", "Mango 馃キ", "Kiwi 馃", "Strawberry 馃崜"];
// Step Five: Add Event Listener For Key Strokes
document.getElementById("searchInput").addEventListener("input", search);
// Step Six: Filter The List Based On User Input
function search() {
const userInput = document.getElementById("searchInput").value.toLowerCase();
const results = fruits.filter(fruit => fruit.toLowerCase().includes(userInput));
displayResults(results);
}
// Step Seven: Display The Results List As A Drop Down
function displayResults(results) {
const suggestionsList = document.getElementById("suggestions");
suggestionsList.innerHTML = "";
results.forEach(result => {
const listItem = document.createElement("li");
listItem.textContent = result;
suggestionsList.appendChild(listItem);
});
}
// Step Eight: Highlight the suggestion below a user鈥檚 cursor
document.getElementById("suggestions").addEventListener("mouseover", highlightSuggestion);
function highlightSuggestion(event) {
const suggestion = event.target;
if (suggestion.tagName === "LI") {
suggestion.classList.add("highlighted");
}
}
// Step Nine: Populate the search box with a user鈥檚 selected suggestion
document.getElementById("suggestions").addEventListener("click", useSuggestion);
function useSuggestion(event) {
const suggestion = event.target;
if (suggestion.tagName === "LI") {
document.getElementById("searchInput").value = suggestion.textContent;
}
}