Skip to content Skip to sidebar Skip to footer

How Can I Apply An Existing CSS Class Style To A Dynamically Created Div?

Apologies for this seemingly redundant question, but the others all seem to be asking about different cases, such as using JQuery. I'm trying to dynamically create a div & appl

Solution 1:

In vanilla JS the property for adding a class to an element is className not class.

Try this instead:

div.className = "tile a";


Solution 2:

What you should do is inspect the HTML after your function has run to make sure that the class is being applied properly. The CSS will apply to the matching element even if it is dynamically created - unless the element isn't being tagged correctly. I suggest using Firebug in Firefox to make sure that your div is correct.


Post a Comment for "How Can I Apply An Existing CSS Class Style To A Dynamically Created Div?"