-
Notifications
You must be signed in to change notification settings - Fork 0
/
kids.js
37 lines (34 loc) · 1018 Bytes
/
kids.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
let bag = [];
fetch("API/kids.json")
.then((res)=>res.json())
.then((data)=>{
bag=data;
displayData(bag);
})
let cartarr=[];
function displayData(bag){
let xyz = document.querySelector(".products");
xyz.innerHTML = "";
bag.forEach((item) => {
let div=document.createElement("div");
div.setAttribute("class","product");
div.innerHTML=
`<img src="${item.avatar}" alt="${item.avatar}">
<p class="title">${item.title}</p>
<p class="price">
<span>₹</span>
<span>${item.price}</span>
</p>
<p class="rating">Rating: ${item.rating}</p>`;
let btn=document.createElement("button");
btn.setAttribute("class","button")
btn.innerText="Add to cart"
btn.addEventListener("click",function(){
alert("Product Added To Cart");
cartarr.push(item)
localStorage.setItem("cart",JSON.stringify(cartarr));
});
div.append(btn);
xyz.append(div);
})
}