-
Notifications
You must be signed in to change notification settings - Fork 0
/
script copy.js
executable file
·118 lines (112 loc) · 2.07 KB
/
script copy.js
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
117
118
const input = document.querySelector('#fruit');
const suggestions = document.querySelector('.suggestions ul');
const fruit = [
'Apple',
'Apricot',
'Avocado 🥑',
'Banana',
'Bilberry',
'Blackberry',
'Blackcurrant',
'Blueberry',
'Boysenberry',
'Currant',
'Cherry',
'Coconut',
'Cranberry',
'Cucumber',
'Custard apple',
'Damson',
'Date',
'Dragonfruit',
'Durian',
'Elderberry',
'Feijoa',
'Fig',
'Gooseberry',
'Grape',
'Raisin',
'Grapefruit',
'Guava',
'Honeyberry',
'Huckleberry',
'Jabuticaba',
'Jackfruit',
'Jambul',
'Juniper berry',
'Kiwifruit',
'Kumquat',
'Lemon',
'Lime',
'Loquat',
'Longan',
'Lychee',
'Mango',
'Mangosteen',
'Marionberry',
'Melon',
'Cantaloupe',
'Honeydew',
'Watermelon',
'Miracle fruit',
'Mulberry',
'Nectarine',
'Nance',
'Olive',
'Orange',
'Clementine',
'Mandarine',
'Tangerine',
'Papaya',
'Passionfruit',
'Peach',
'Pear',
'Persimmon',
'Plantain',
'Plum',
'Pineapple',
'Pomegranate',
'Pomelo',
'Quince',
'Raspberry',
'Salmonberry',
'Rambutan',
'Redcurrant',
'Salak',
'Satsuma',
'Soursop',
'Star fruit',
'Strawberry',
'Tamarillo',
'Tamarind',
'Yuzu',
];
function search(str) {
let results = [];
fruit.filter((value) =>
value.toLowerCase().includes(str) ? results.push(value) : ''
);
return results;
}
function searchHandler(event) {
let inputValue = event.target.value.toLowerCase();
let results = search(inputValue);
inputValue ? showSuggestions(results) : (suggestions.innerText = '');
}
function showSuggestions(results) {
suggestions.innerText = '';
if (results.length > 0) {
for (let i = 0; i < results.length; i++) {
const newLi = document.createElement('li');
newLi.innerText = results[i];
suggestions.appendChild(newLi);
}
}
}
function useSuggestion(event) {
const suggestionValue = event.target.innerText;
input.value = suggestionValue;
suggestions.innerText = '';
}
input.addEventListener('keyup', searchHandler);
suggestions.addEventListener('click', useSuggestion);