I want to convert the source of the website inside a String variable to code and use it.
How can I do this?
var cssStr = 'a{text-decoration: none}';
// to css code
a {
text-decoration: none
}
I want to convert the source of the website inside a String variable to code and use it.
How can I do this?
var cssStr = 'a{text-decoration: none}';
// to css code
a {
text-decoration: none
}
You can have below script:
var cssStr = 'a{text-decoration: none}';
let styleTag = document.createElement('style');
styleTag.innerHTML = cssStr;
document.firstChild.appendChild(styleTag);
This will basically create <style></style> and append it to html. If you have specific <style> tag, select it instead of creating new.
This is what CSSStyleSheet.insertRule is for:
let styleSheet = document.styleSheets[0];
styleSheet.insertRule('.demo { background: red; color: white; }');
<div class="demo">test</div>