-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
83 lines (53 loc) · 1.55 KB
/
main.js
File metadata and controls
83 lines (53 loc) · 1.55 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
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
};
function fetchFunction(){
fetch("https://dummyjson.com/products").then((result) => {
let myData = result.json();
console.log(myData);
return myData;
}).then((myData) => {
let res= myData.products;
res.length=28;
console.log(res);
return res;
}).then((res) =>{
let data="";
res.map((values)=>{
data+=`<div class="card">
<img src=${values.images[0]} alt="" class="images img-fluid">
<h3 class="title"> ${values.title} </h3>
<p class="price"><span class="fw-bold">Price:</span> ${values.price}</p>
<button id ="btn" onclick="showDetailsButton(${values.id})" >Show Details</button>
</div>`
})
document.getElementById("cards").innerHTML=data;
document.getElementById("mobileCards").innerHTML=data;
}
).catch((err) =>{
console.log(err);
});
}
fetchFunction();
function searchElement() {
let input = document.getElementById('searchbar').value
input=input.toLowerCase();
let x = document.getElementsByClassName('card');
if(input){
for (i = 0; i < x.length; i++) {
if (!x[i].innerHTML.toLowerCase().includes(input)) {
x[i].style.display="none";
}
else {
x[i].style.display="card";
}
}
}
else{
fetchFunction();
}
}
function showDetailsButton(idValue){
window.location.href=`product.html?id=${idValue}`;
};