- 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
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");
+ 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)
0
I still want to try it. Probably won't use it, but still.
do you know how?