How to dynamically remove a stylesheet from the current page

JavascriptJqueryCssStylesheet

Javascript Problem Overview


Is there a way to dynamically remove the current stylesheet from the page?

For example, if a page contains:

<link rel="stylesheet" type="text/css" href="http://..." />

...is there a way to later disable it with JavaScript? Extra points for using jQuery.

Javascript Solutions


Solution 1 - Javascript

Well, assuming you can target it with jQuery it should be just as simple as calling remove() on the element:

$('link[rel=stylesheet]').remove();

That will remove all external stylesheets on the page. If you know part of the url then you can remove just the one you're looking for:

$('link[rel=stylesheet][href~="foo.com"]').remove();

And in Javascript

this is an example of remove all with query selector and foreach array

Array.prototype.forEach.call(document.querySelectorAll('link[rel=stylesheet]'), function(element){
      try{
        element.parentNode.removeChild(element);
      }catch(err){}
    });

//or this is similar
var elements = document.querySelectorAll('link[rel=stylesheet]');
for(var i=0;i<elements.length;i++){
    elements[i].parentNode.removeChild(elements[i]);
}

Solution 2 - Javascript

If you know the ID of the stylesheet, use the following. Any other method of getting the stylesheet works as well, of course. This is straight DOM and doesn't require using any libraries.

var sheet = document.getElementById(styleSheetId);
sheet.disabled = true;
sheet.parentNode.removeChild(sheet);

Solution 3 - Javascript

I found this page whilst looking for a way to remove style sheets using jquery. I thought I'd found the right answer when I read the following

>If you know part of the url then you can remove just the one you're looking for: $('link[rel=stylesheet][href~="foo.com"]').remove();"

I liked this solution because the style sheets I wanted to remove had the same name but were in different folders. However this code did not work so I changed the operator to *= and it works perfectly:

$('link[rel=stylesheet][href*="mystyle"]').remove();

Just thought I'd share this in case it's useful for someone.

Solution 4 - Javascript

This will disable any stylesheet matching the regular expression searchRegEx provided against the URL of each stylesheet.

let searchRegEx = /example.*/;

for (var i = 0; i < document.styleSheets.length; i++) {
    if (document.styleSheets[i].href.search(searchRegEx) != -1) {
        document.styleSheets[i].disabled = true;
    }
}

Solution 5 - Javascript

Nobody has mentioned removing a specific stylesheet without an ID in plain Javascript:

 document.querySelector('link[href$="something.css"]').remove()

("$=" to find at end of href)

Solution 6 - Javascript

This will reset your page's styling, removing all of the style-elements. Also, jQuery isn't required.

Array.prototype.forEach.call(document.querySelectorAll('style,[rel="stylesheet"],[type="text/css"]'), function(element){
  try{
    element.parentNode.removeChild(element)
  }catch(err){}
});

Solution 7 - Javascript

This is for disable all <style> from html

// this disable all style of the website...
var cant = document.styleSheets.length
for(var i=0;i<cant;i++){
    document.styleSheets[i].disabled=true;
}

//this is the same disable all stylesheets
Array.prototype.forEach.call(document.styleSheets, function(element){
  try{
    element.disabled = true;
  }catch(err){}
});

Solution 8 - Javascript

To expand on Damien's answer, the test method (which returns true or false) would probably be a better fit than search and is slightly faster. Using this method would yield:

for (var i = 0; i < document.styleSheets.length; i++) {
    if (/searchRegex/.test(document.styleSheets[i].href)) {
        document.styleSheets[i].disabled = true;
    }
}

If you don't care about IE support this can be cleaned up with a for...of loop

for (const styleSheet of document.styleSheets) {
    if (/searchRegex/.test(styleSheet)) {
        styleSheet.disabled = true;
    }
}

Solution 9 - Javascript

Suppose you want to remove a class myCssClass then the most easy way to do it is element.classList.remove("myCssClass");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan OsmanView Question on Stackoverflow
Solution 1 - JavascriptBrian DonovanView Answer on Stackoverflow
Solution 2 - JavascriptSir RobertView Answer on Stackoverflow
Solution 3 - Javascriptuser5936810View Answer on Stackoverflow
Solution 4 - JavascriptDamien BezborodowView Answer on Stackoverflow
Solution 5 - JavascripteonView Answer on Stackoverflow
Solution 6 - Javascriptuser257319View Answer on Stackoverflow
Solution 7 - JavascriptDarckBlezzerView Answer on Stackoverflow
Solution 8 - JavascriptjrgermainView Answer on Stackoverflow
Solution 9 - JavascriptAtashG79View Answer on Stackoverflow