I have a page containing a select tag that allow to change the css theme of the page "on the fly" with some javascript.
Now, I want to allow to change the css style for specifics html elements in the page.
By example, I have theses two 'theme' stylesheets :
/* in red theme file 'theme_red.css' */
.title{color:red;}
.text{...}
/* in blue theme file 'theme_blue.css' */
.title{color:blue;}
.text{...}
And I have some "select" tag "change theme" in the page. This is here a minimalist example, in a real case, the number of 'themable' html elements and the number of stylesheet are modular.
I'm looking for create a javascript function like this : (pure javascript ES6)
loadCssThemeFileOnTheFlyForSpecificHtmlElement(idOfSpecificHtmlElement, themeName){
// 1) Insert a new link tag in the head of the page with href attribute "themeName + '.css'"
...
// 2) for each css rules selector (of the css file ?), override the css selector rule like this :
// `.selector1` become `#idOfElement .selector1`
// `.selector2` become `#idOfElement .selector2`
// ...
// `.selectorN` become `#idOfElement .selectorN`
}
And I'm stuck in the step 2 of the function insertCssFileOnTheFly. Is it possible to do what I want to do with some javascript ?
I mean, is it possible to loop of the css rule of the current page and add to each selector rule the string "#idOfElement " ?
Thanks for any help