I am using JavaScript. I have a variable var boolVal that either evaluates to true/false.
On my page, I have a div tag.
You can add a css class based on id dynamically as follows:
document.getElementById('idOfElement').className = 'newClassName';
or using classList
document.getElementById('idOfElement').classList.add('newClassName');
Alternatively you can use other DOM methods like
etc to find elements. The last three return a collection so you'll have to iterate over it and apply the class to each element in the collection.
In your case
var element = document.getElementById('div1');
if(boolVal)
element.className= 'blueClass'; // += ' blueClass'; to keep existing classes
else
element.className= 'redClass';