I have JavaScript code that creates some elements div, and it sets its CSS properties. Since I would like to separate CSS logic from JavaScript code and because CSS is easier to read in my own .cssfile, I would like to set the CSS of classNamemy element and then dynamically insert some values into a specific CSS property.
Here is what I would like to do:
style.css:
.myClass {
width: $insertedFromJS
}
script.js:
var myElement = document.createElement("div");
myElement.className = "myClass";
I want to do something like this, but at this point is myElement.style.widthempty
myElement.style.width.replaceAll("$insertedFromJS", "400px");
I think my problem here is that after calling myElement.className = "myClass"CSS, it still doesn't apply.
source
share