document.getElementsByClassName('myclass');
will return an array of elements that match.
You can also use something in the lines:
<body> <div id="changethese"> <div class="myclass" onclick="change_class()">Some Text</div> </div> <div class="myclass">div two</div> </body>
JavaScript:
var changeTheseDivs = document.getElementById('changethese'); changeTheseDivs.getElementsByClassName('myclass')
To change only the class inside the selected div.
However, support for this method only works in IE9, so if jQuery is an option, it is best to use this.
Edit:
I believe that I am reading incorrectly, it looks like you want to change the CSS rule itself, and not change the CSS on the elements. Theoretically, you could write a function that will find rules for you by name and change their properties. I assume that it will look something like this (untested, should work):
function findAndChangeCSSRule(ruleName, newRule, newProperty){ var mysheet=document.styleSheets[0]; var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules for (i=0; i<myrules.length; i++){ if(myrules[i].selectorText.toLowerCase()==ruleName){ targetrule=myrules[i]; break; } } targetrule.style[newRule.toString()]=newProperty; }
then name it with
findAndChangeCSSRule('my_class', 'color', 'red')
Chris sobolewski
source share