forked from vnmrsharma/Treehacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (32 loc) · 1.06 KB
/
script.js
File metadata and controls
39 lines (32 loc) · 1.06 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
const selected = document.querySelector(".selected");
const optionsContainer = document.querySelector(".options-container");
const searchBox = document.querySelector(".search-box input");
const optionsList = document.querySelectorAll(".option");
selected.addEventListener("click", () => {
optionsContainer.classList.toggle("active");
searchBox.value = "";
filterList("");
if (optionsContainer.classList.contains("active")) {
searchBox.focus();
}
});
optionsList.forEach(o => {
o.addEventListener("click", () => {
selected.innerHTML = o.querySelector("label").innerHTML;
optionsContainer.classList.remove("active");
});
});
searchBox.addEventListener("keyup", function(e) {
filterList(e.target.value);
});
const filterList = searchTerm => {
searchTerm = searchTerm.toLowerCase();
optionsList.forEach(option => {
let label = option.firstElementChild.nextElementSibling.innerText.toLowerCase();
if (label.indexOf(searchTerm) != -1) {
option.style.display = "block";
} else {
option.style.display = "none";
}
});
};