Skip to content
Advertisement

How do I make it so that when I click it changes the list

I have a list full of colors; I’m trying to make it so that when I click a button, it moves over one position in a list, thus changing the color of a shirt. How would I make it so that when I click the button it shifts to the next value?

e.g.

JavaScript

(I know this is wrong, but do you know what I should do?)

When you click button, the car should turn from red to blue. click again, blue to white, etc.

Thanks!

Advertisement

Answer

As per @Barmar recommendation, here is the code:

JavaScript

Keep in mind clicking after “orange” will cause an error, as there are no more colors. I would recommend adding a guard for that instance.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement