Home » Javascript » Is there a way of not only adding/removing a class to an HTML element but actually change the value within a CSS class?

Is there a way of not only adding/removing a class to an HTML element but actually change the value within a CSS class?

Posted by: admin August 19, 2018 Leave a comment

Questions:

As for example: I wanna show random background colors by pressing a button. I greated variables which would result in a usual hex-code if I put the strings together… Only how can I write these random values into the CSS file? Is there any way?

I do not wanna change the class name, but what the class name contains as a style-value for background-color!

function changeColor() {
var content = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g"];
var one = content[Math.floor(Math.random()*content.length)];
var two = content[Math.floor(Math.random()*content.length)];
var three = content[Math.floor(Math.random()*content.length)];
var four = content[Math.floor(Math.random()*content.length)];
var five = content[Math.floor(Math.random()*content.length)];
var six = content[Math.floor(Math.random()*content.length)];
Answers: