Search⌘ K

Solution: Colorful button

Explore how to build a colorful button that changes the webpage background color. Learn to use arrays, variables, and event listeners to cycle through colors and control page style dynamically.

We'll cover the following...

Colorful button

Let’s create a task using a loop and an array of colors. Upon clicking a button, the background color should rotate through the colors stored in an array ["white", "lightblue", "lightgreen", and "lavender"].

Each time the user clicks the button:

  • The background color changes to the next color in the array.

  • When the last color is reached, the cycle starts again from the beginning.

You’ll ...