Skip to content
Advertisement

Random color on different div’s

I have 3 div’s

JavaScript

I want to give it a random color using javascript controlled css. Like this:

JavaScript

The problem is that, it’s giving me the same color in every div. Any idea how can i solve this, I want to do it pure javascript and css no jquery if possible. Im still learning javascript. Thank you..

Advertisement

Answer

You could use classes instead of ids and simplify your code to following.

JavaScript
JavaScript

Randomizing the colors on page refresh/load.

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