I have to access CSS class by name, and the code below works. However if I try hui["myclass"] or hui[".myclass"] instead of hui[0] it cannot find it.
function change_class() {
var hui = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
hui[0].style["color"] = "#ff0000"
}
.myclass {
color: #00aa00;
}
<div class="myclass" onclick="change_class()">Some Text</div>
EDIT: I need to be able to acess as i described on line 1 i dont want to acess trough individual elements on page, but directly to stylesheet by class name.
So you all saying i cannot access to stylesheet by class name only by index?
No, you can’t access them by the selector – it’s a simple list. You first had to build an index for it:
Of course this is not a fool-proof method, there could be
.myClass, .myOtherClassand instead of blindly assigning the
colorproperty you first should check for existence of the declaration.