Monday, May 01, 2023

Change CSS class from JavaScript

StyleSheetList - Web APIs | MDN


javascript - Change a CSS stylesheet's selectors' properties - Stack Overflow

// ssMain is the stylesheet's index based on load order. See document.styleSheets. E.g. 0=reset.css, 1=main.css.
var ssMain = 1;
var cssRules = (document.all) ? 'rules': 'cssRules';
function changeCSSStyle(selector, cssProp, cssVal) {
  for (i=0, len=document.styleSheets[ssMain][cssRules].length; i<len; i++) {
    if (document.styleSheets[ssMain][cssRules][i].selectorText === selector) {
      document.styleSheets[ssMain][cssRules][i].style[cssProp] = cssVal;
      return;
    }
  }
}



No comments: