I am currently working on dynamically changing the CSS style (specifically background-color) for certain text on a webpage. To achieve this, I have been attempting to modify CSSRule by accessing the desired CSSRule based on selectedText. Here is the code snippet:
changeRule() {
let stylesheet: CSSStyleSheet = <CSSStyleSheet>this.stylesheet.sheet;
let ruleLength: any = <CSSStyleSheet>this.stylesheet.sheet.cssRules.length;
console.log(ruleLength)
for(let i = 0; i < ruleLength; i++) {
let currentTag: any = this.stylesheet.sheet.cssRules[i].selectorText as CSSStyleSheet
let chosenTag: any = (`#text `+`[data-tag-id="${this.selectedTag.id}"]`)
if(currentTag === chosenTag) {
console.log("Rule matched");
console.log(<CSSStyleSheet>this.stylesheet.sheet.cssRules[i].style.backgroundColor);
let changedTag: any = <CSSStyleDeclaration>this.stylesheet.sheet.cssRules[i].style.backgroundColor
changedTag = "initial";
break;
}
}
console.log(<CSSStyleSheet>this.stylesheet.sheet)
}
However, despite running this function, the corresponding CSSRule does not seem to be updated with the new BackgroundColor. Furthermore, I am encountering a compile time error related to cssRules[i] stating that "Property cssRules does not exist on type StyleSheet". It's worth mentioning that I am using angular2 with typescript.
If anyone has any suggestions or insights on how to tackle this issue, please feel free to share them with me!