This repository was archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (65 loc) · 1.93 KB
/
app.js
File metadata and controls
76 lines (65 loc) · 1.93 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
let list = {
title: [],
link: []
}
for (const index of document.querySelectorAll("a")) {
list.title.push(index.innerText);
list.link.push(index.href)
}
// Make a search panel that displays results when invoked.
const sPanel = document.createElement("div");
sPanel.style.display = "none"
sPanel.id = "sPanel"
document.querySelector('.box').appendChild(sPanel)
// Make a <ul> element for future search results. Hide it for now.
const sList = document.createElement("ul")
sList.id = "sList"
sPanel.appendChild(sList)
// Populate the <ul> with <li>s and links from the HTML page. Hide for now.
for (let i=0; i <= list.title.length-1; i++){
const li = document.createElement("li");
li.style.display = "none"
const a = document.createElement("a");
const aNode = document.createTextNode(list.title[i])
// Append text node to anchor element:
a.appendChild(aNode)
a.title = list.title[i];
a.href = list.link[i];
sList.appendChild(li);
li.appendChild(a);
}
// Dynamically show and hide each <li> based on search() results.
function search() {
let searchbox = document.getElementById("search-item").value.toUpperCase();
let storeitems = document.querySelector('.product-list');
let sList = document.querySelector('#sList');
if (searchbox == "") {
storeitems.style.display = ""
sPanel.style.display = "none"
} else {
storeitems.style.display = "none"
sPanel.style.display = ""
}
// Iterate through the list.title array:
for (let i = 0; i < list.title.length; i++) {
let title = list.title[i].toUpperCase();
let li = sList.children[i];
if (title.includes(searchbox)) {
// If a match is found, display it.
li.style.display = ""
} else {
li.style.display = "none";
}
}
}
const searchbar = document.getElementById("search-item")
document.addEventListener("keydown", (event) => {
if (event.key === "/") {
searchbar.focus();
event.preventDefault();
}
if (event.key === "Escape"){
searchbar.focus();
searchbar.value = ""
}
});