-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
30 lines (29 loc) · 881 Bytes
/
main.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
function displayProduct(product) {
let card = document.createElement("li")
card.className = "card col-2 m-2"
card.innerHTML = `
<img src="${product.image}" class="card-img-top" alt="${product.title}">
<div class="card-body">
<h5 class="card-title">${product.title}</h5>
<p class="card-text">${product.description}</p>
<h6 class="card-title">
Ksh:<span>${product.price}</span>
</h6>
<a class="btn btn-primary">Buy</a>
</div>
`
//add the card to the DOM
document.querySelector("#item-list").append(card)
}
//fetch product data
function getProducts() {
fetch(" http://localhost:3000/products")
.then(res => res.json())
.then((products) => {
// console.log(products)
products.forEach((product) => {
displayProduct(product)
})
})
}
getProducts()