-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.js
51 lines (44 loc) · 1.72 KB
/
counter.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
/* DOM */
const explainerText = document.querySelector(".explainer-text"); // This is your explainer text.
const modeBtn = document.querySelector(".mode-btn"); // This is your mode button.
const minus = document.querySelector(".minus"); // This is your minus button.
const inc = document.querySelector("#inc"); // This is the number text.
const plus = document.querySelector(".plus"); // This is your plus button.
const reset = document.querySelector(".reset"); // This is your reset button.
let i = 0; // This is your variable for the number.
/* Event Handler */
// When this is clicked, toggle light/dark mode
modeBtn.addEventListener("click", toggleChange);
// When this is clicked, increase the number text by 1.
minus.addEventListener("click", decrease);
// When this is clicked, decrease the number text by 1.
plus.addEventListener("click", increase);
// When this is clicked, reset the number text back to 0.
reset.addEventListener("click", resetNumber);
/* Functions */
// Increase the number text by 1.
function increase() {
i++
updateNumberText();
};
// Decrease the number text by 1.
function decrease() {
i--;
updateNumberText();
};
// Reset the number text back to 0.
function resetNumber() {
i = 0;
updateNumberText();
}
// This will update the number text.
function updateNumberText() {
inc.value = i;
}
// This will toggle dark mode and light mode.
function toggleChange() {
document.body.classList.toggle("dark-mode");
// This will change the text depending on the mode.
if (document.body.classList.contains("dark-mode")) explainerText.textContent = "Click to enable light mode.";
if (!document.body.classList.contains("dark-mode")) explainerText.textContent = "Click to enable Dark Mode.";
}