CSS styles doesn\'t work on dynamically created elements in IE

Blog / JavaScript ·

Again, IE proves that is full with bugs. Simple but effective solution.

If you have the following code:

var element = document.createElement("div");
element.setAttribute("class", "yourClassName");

change it to:

var element = document.createElement("div");
element.className = "yourClassName";

.className property also works in FF, Opera and Safari.


Krasimir Tsonev With over two decades of deep programming expertise, I offer comprehensive web consultancy and stack audits, alongside specialized workshops, training, and engaging public speaking to elevate your team's skills and optimize your digital presence. Contact me.

Keywords: createelement div element document createelement div element document createelement var element document
Share the post on Twitter, Facebook, LinkedIn