My Set Up
I have a pie chart css that I use to display pie charts in percentage,
The class works this way:
<div id="chart" class="c100 p20 small dark center">
The second class "p20" means it should display a chart of 20%, thus if i change it to "p70", is shows a 70% chart.
Now I have also a Java Script code to generate a random number and display it in a div,
setInterval(function() {
var random = (Math.floor((Math.random() * 100) + 1));
document.getElementById("demo").innerHTML = random;
}, 1000);
It works
What I want
But i also want the random number to change the class of the chart div, such as
if the random number is
30, then the class of the chart changes top30<div id="chart" class="c100 p30 small dark center">,if the random number is
80, then the class of the chart changes top80<div id="chart" class="c100 p80 small dark center">
I've tried this, but it doesn't work, it only updates with the first random number and stops.
setInterval(function() {
var random = (Math.floor((Math.random() * 100) + 1));
document.getElementById("demo").innerHTML = random;
//remove the default class and add the new class
document.getElementById("chart").classList.remove('p20');
document.getElementById("chart").classList.add('p' + random);
}, 1000);
randeclared in your code, did you mean'p' + random?document.getElementById("chart").classList.remove('p20');This will do nothing if after the first time you add something likep50.