How to load/unload css dynamically

Viewed 554

I know how to load CSS.

injectCss(styles) {
  let styleSheet = document.createElement("style");
  styleSheet.type = 'text/css';
  styleSheet.innerText = styles;
  document.head.appendChild(styleSheet);
}

How to unload it?

1 Answers

Hope this help you.

function injectCss(styles) {
  let styleSheet = document.createElement("style");
  styleSheet.type = 'text/css';
styleSheet.setAttribute("id", "dunamicstylesheet");
  styleSheet.innerText = styles;
  document.head.appendChild(styleSheet);
}

//Create-styleSheet
injectCss('dynamic.css');

//remove-styleSheet
var stylesheet = document.getElementById('dunamicstylesheet');
stylesheet.parentNode.removeChild(stylesheet);
Related