-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (38 loc) · 1.33 KB
/
index.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
const selectDiv = document.getElementById('root')
selectDiv.addEventListener('click', upvote)
function loadProductList() {
const xhr = new XMLHttpRequest()
//open
xhr.open('GET', 'data.json', true)
//onload
xhr.onload = function() {
if (this.status == 200) {
const productsList = JSON.parse(this.response)
let output = ''
productsList.forEach(function(product) {
output += `
<div class='col-sm-5 shadow p-3 mb-5 bg-white rounded media' style="text-align: center">
<img src=${product.image} class="align-self-center mr-3" alt="...">
<div class="media-body">
<h4 class="mt-1">${product.productName}</h4>
<h5 class="mt-1">${product.description}</h5>
<p class="mb-1">${product.votes}</p>
<button class="mb-0 upBt" id=${product.id}> Upvote </button>
</div>
</div>
`
})
selectDiv.innerHTML = output
}
}
//send
xhr.send()
}
loadProductList()
function upvote(e) {
if (e.target.classList.contains('upBt')) {
const value = e.target.parentElement
const updateVote = (value.querySelector('p'))
updateVote.textContent = Number(updateVote.textContent) + 1
}
}