-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage1.js
49 lines (42 loc) · 1.26 KB
/
page1.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
const stores = [
"Coles",
"Woolworths",
"Target",
"David Jones",
"Big W"
];
const searchInput = document.getElementById("searchInput");
const searchButton = document.getElementById("searchButton");
const storeList = document.getElementById("storeList");
searchButton.addEventListener("click", searchStores);
searchInput.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
searchStores();
}
});
function searchStores() {
const searchTerm = searchInput.value.toLowerCase();
const matchingStores = stores.filter(store =>
store.toLowerCase().includes(searchTerm)
);
displayStores(matchingStores);
}
function displayStores(stores) {
storeList.innerHTML = "";
if (stores.length === 0) {
const listItem = document.createElement("li");
listItem.textContent = "No stores found.";
storeList.appendChild(listItem);
} else {
stores.forEach(store => {
const listItem = document.createElement("li");
const button = document.createElement("button");
button.textContent = store;
button.addEventListener("click", () => {
console.log(store);
});
listItem.appendChild(button);
storeList.appendChild(listItem);
});
}
}