-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlikeTracker.js
60 lines (51 loc) · 2.24 KB
/
likeTracker.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
56
57
58
59
60
//secret codes section---------------------------------------------------
//get all like and dislike buttons
const likeButtons = document.querySelectorAll('.likeBtn');
const dislikeButtons = document.querySelectorAll('.dislikeBtn');
//set a reference to the like counter description
const likeCounterDescription = document.querySelector('.likeCounterDescription');
let likes = 0;
let dislikes = 0;
//update the text
function updateDescription() {
likeCounterDescription.textContent = `You Liked ${likes} items and hated ${dislikes} items`;
}
//add event listeners to like buttons
likeButtons.forEach(button => {
button.addEventListener('click', () => {
likes++;
updateDescription();
button.parentNode.parentNode.style.backgroundColor = 'rgb(123, 175, 170)';
//append a message to the bottom of screen, so users can keep track of what they liked and didnt
const refToMessages = document.querySelector('.messages');
const message = document.createElement('p');
var code = button.parentNode.parentNode.querySelector('strong').innerText.replace('code: ', ''); //get rid of "code" at begginning of each code
message.textContent = `You liked ${code}`;
refToMessages.appendChild(message);
});
});
//add event listeners to dislike buttons
dislikeButtons.forEach(button => {
button.addEventListener('click', () => {
dislikes++;
updateDescription();
button.parentNode.parentNode.style.backgroundColor = 'rgb(208, 91, 91)';
//append a message to the bottom of screen, so users can keep track of what they liked and didnt
const refToMessages = document.querySelector('.messages');
const message = document.createElement('p');
var code = button.parentNode.parentNode.querySelector('strong').innerText.replace('code: ', '');
message.textContent = `You hated ${code}`;
refToMessages.appendChild(message);
});
});
// Add event listener to start over button
document.querySelector('.startover').addEventListener('click', () => {
likes = 0;
dislikes = 0;
updateDescription();
document.querySelectorAll('.codeElement').forEach(div => {
div.style.backgroundColor = 'initial'; // Reset background color
});
//clear all messages made from buttons
document.querySelector('.messages').innerHTML = '';
});