How to add CSS class to element dynamically in JavaScript

preview_player
Показать описание
(We're free online community, meet other makers!)

To add class to element dynamically in js there are actually 3 ways of doing that. This tutorial will demonstrate 4 of them, based on your situation you should just see for yourself which use case works best for you.
Рекомендации по теме
Комментарии
Автор

Can I ask how would you do this if you wanted to create 'div' elements using I want to create the 'div' elements dynamically and then remove them using remove() or removeChild() for a card game. I wanted to add a border to the 'div' elements that I created by either writing parentDiv.style.border = "dodgerblue" in the JS file or by following your first example and placing the CSS in between two <style></style> tags and the JS code in two <script></script> tags in the HTML file but neither method worked. Do you have any suggestions?

atsushiidobe
Автор

I can't see the last bit because of the cards popping up over the content before the video is finished. If you're going to use them - at least wait until the actual content that I cane here for is finished!

markcameron-smith