Adding classes automatically with js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Adding classes automatically with js?

Is there a way to automatically add classes to elements with js? I'm writing a blog and I'm stuck manually adding classes to every div. https://code.sololearn.com/WuqZ0n7eh9Vv/?ref=app

6th May 2018, 5:18 AM
Daniel Cooper
Daniel Cooper - avatar
3 Answers
+ 1
Thats the best way,manually adding each class to its elements.Adding classes with js is more stressful and is way less neat.Also you could encounter problems since the Dom has to load before you can even add those classes Daniel Cooper this is the jquery syntax:: $("element").attr("class","className");
6th May 2018, 5:22 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
If you want apply a style to an element list that follow an pattern you can use special selectors... At example in your case you can use: body > div{ ..... } and all your div that are direct childs of body element will get that rules (but its not suggested, its better type class and make all more specified)
6th May 2018, 7:52 AM
KrOW
KrOW - avatar
0
I still want to try it. Probably won't use it, but still. do you know how?
6th May 2018, 5:24 AM
Daniel Cooper
Daniel Cooper - avatar