-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
78 lines (56 loc) · 2.27 KB
/
actions.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
// -------------------------------------------------------------
// HERE IS THE SEARCH BAR CODE PART
// -------------------------------------------------------------
// load from json
let circuits;
function loadDatabase() {
const xhttp = new XMLHttpRequest();
xhttp.open('GET', 'database.json', true);
xhttp.send();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
circuits = JSON.parse(this.responseText);
for (let index = 0; index < circuits.length; index++) {
selectElement(".gallery").innerHTML += `
<div class="gallery-item">
<a href="${circuits[index].circuitCode}">
<img class="gallery-image" src= "${circuits[index].circuitImg}" alt="${circuits[index].circuitAlt}" title="${circuits[index].circuitAlt}"/>
</a>
</div>
`;
}
}
}
}
// short for query selector
function selectElement(selector) {
return document.querySelector(selector);
}
// show all items when page load
document.addEventListener("DOMContentLoaded", loadDatabase());
// get the results
function getResults() {
// get the element to search
const search = selectElement(".searchTerm").value;
selectElement(".gallery").innerHTML = "";
// loop into the searhc term
for (let index = 0; index < circuits.length; index++) {
if (circuits[index].circuitTags.toLocaleLowerCase().includes(search.toLocaleLowerCase())) {
selectElement(".gallery").innerHTML += `
<div class="gallery-item">
<a href="${circuits[index].circuitCode}">
<img class="gallery-image" src= "${circuits[index].circuitImg}" alt="${circuits[index].circuitAlt}" title="${circuits[index].circuitAlt}"/>
</a>
</div>
`;
}
}
}
// triggering the search
selectElement(".searchTerm").addEventListener("keyup", getResults);
// cleaning the search
selectElement(".searchButton").addEventListener("click", () => {
var search = selectElement(".searchTerm");
search.value = "";
getResults();
});