-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (25 loc) · 1.04 KB
/
Copy pathscript.js
File metadata and controls
32 lines (25 loc) · 1.04 KB
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
const textElement = document.querySelector("#name");
const button = document.querySelector("#btn");
// Initialize a click counter
let clickCount = 0;
// Fetch the colors from the JSON file
fetch('colors.json')
.then(response => response.json())
.then(data => {
const colors = data.colors;
// Function to handle button clicks
function handleButtonClick() {
// Increment the click counter
clickCount++;
// Determine the color based on the click count
const colorIndex = (clickCount - 1) % colors.length; // Use modulo to cycle through colors
const selectedColor = colors[colorIndex];
// Change the background color
document.body.style.backgroundColor = selectedColor;
// Log the current color to the console (optional for debugging)
console.log(`Click ${clickCount}: Color changed to ${selectedColor}`);
}
// Attach the event listener to the button
button.addEventListener("click", handleButtonClick);
})
.catch(error => console.error('Error loading colors.json:', error));