-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
55 lines (45 loc) · 1.71 KB
/
search.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
<script>
function showResult(str) {
if (str.length === 0) {
document.getElementById("livesearch").innerHTML = "";
document.getElementById("livesearch").style.border = "0px";
return;
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
displayResults(this.responseXML, str);
}
};
xhttp.open("GET", "search.xml", true);
xhttp.send();
}
function displayResults(xml, searchStr) {
var livesearchDiv = document.getElementById("livesearch");
livesearchDiv.innerHTML = "";
var products = xml.getElementsByTagName("product");
for (var i = 0; i < products.length; i++) {
var title = products[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
var description = products[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
var image = products[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
if (title.toLowerCase().includes(searchStr.toLowerCase())) {
var resultItem = document.createElement("div");
resultItem.className = "result-item";
var imgElement = document.createElement("img");
imgElement.src = image;
imgElement.alt = title;
var titleElement = document.createElement("h3");
titleElement.textContent = title;
var descriptionElement = document.createElement("p");
descriptionElement.textContent = description;
resultItem.appendChild(imgElement);
resultItem.appendChild(titleElement);
resultItem.appendChild(descriptionElement);
livesearchDiv.appendChild(resultItem);
}
}
if (livesearchDiv.innerHTML === "") {
livesearchDiv.innerHTML = "No results found.";
}
}
</script>