I use materialize CSS and generate checkboxes dynamically. I want different checkboxes to have different colors. When I'm trying to set the color inline, it doesn't take effect:
<input type="checkbox" class="filled-in checkbox-custom" style="background-color: red;" />
What does work, is setting the following style in my .css file like so:
.checkbox-custom[type="checkbox"].filled-in:checked + span:not(.lever):after {
background-color: red;
}
But this way, I can't set up a color dynamically per each individual checkbox, and I have many colors, so creating a class per each color is not an option.
I tried using css variables in the following manner, in my .css file:
:root {
--checkbox-color: grey;
}
.checkbox-custom[type="checkbox"].filled-in:checked+span:not(.lever):after {
background-color: var(--checkbox-color);
}
And creating the checkbox dynamically in JS as inner html using
container.innerHTML = '<input type="checkbox" class="filled-in checkbox-custom" style="--checkbox-color: ' + myJSColorVar + ';" />';
But the checkbox stays grey and the variable I pass is ignored.
Is there a way to set the checkbox color dynamically from inline CSS without setting a custom class per each color? I've reviewed the following questions which helped me getting started How to pass parameters to css classes, Change color of checkbox in Materialize framework but still weren't able to combine the two concepts.
Thanks!