54

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.

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
  • Duplicate of [Removing or replacing a stylesheet (a ) with JavaScript/jQuery](http://stackoverflow.com/q/3182840/578288). See also [jQuery – enable/disable all style sheets on page on click](http://stackoverflow.com/q/15824089/578288). – Rory O'Kane Sep 20 '13 at 18:45
  • Possible duplicate of [Removing or replacing a stylesheet (a ) with JavaScript/jQuery](https://stackoverflow.com/questions/3182840/removing-or-replacing-a-stylesheet-a-link-with-javascript-jquery) – Francisco Puga Aug 22 '17 at 12:51

9 Answers9

72

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]);
}
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
Brian Donovan
  • 8,274
  • 1
  • 26
  • 25
  • 7
    *[facepalm]* I totally forgot about the attribute selector. – Nathan Osman Feb 17 '11 at 19:39
  • 5
    According to [this question](http://stackoverflow.com/q/3182840/578288), just removing the `link` element is not enough. You should make it [`disabled`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement#Properties) first, with [`.prop('disabled', true)`](http://api.jquery.com/prop/). Though it’s possible that browsers have changed since that question was written (2010), and you don’t need to disable the `link`. – Rory O'Kane Sep 20 '13 at 18:45
  • FWIW Rory, I just opened the DevTools panel in Chrome and selected the stylesheet link for this page and pressed delete and all the styling went away. I suppose it's possible Chrome is doing something special-casey but I doubt it. – Brian Donovan Oct 01 '13 at 21:32
  • `.remove()` doesnt seem to affect the page, see http://binternet.co.il/contact-form/ and click on `rtl` button twice, its stuck on RTL...any idea why? – Broshi Jun 12 '17 at 09:54
  • Dude, you made my day!! Thanks a lot!! – Nishith Kant Chaturvedi Jul 28 '17 at 08:39
  • $('link[rel=stylesheet][href~="stylesheet-link"]').remove(); works fine but is there a way to remove stylesheet irrespective of the version's in it? Like style.css?ver=1.0.1 and the version will be changing – rahul patel Jun 26 '20 at 10:11
  • @rahulpatel just do a string check on the href attribute when looping over the elements – That Realty Programmer Guy Aug 12 '21 at 10:58
28

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);
Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
Sir Robert
  • 4,686
  • 7
  • 41
  • 57
11

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.

user5936810
  • 121
  • 1
  • 3
  • I found the accepted answer did not work for me, using * instead of ~ did the trick . https://stackoverflow.com/questions/14940706/tilde-in-jquery-selector – Tristanisginger Apr 04 '19 at 08:21
8

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;
    }
}
Damien
  • 1,140
  • 15
  • 22
6

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)

eon
  • 1,868
  • 1
  • 23
  • 24
3

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){}
});
Cimbali
  • 11,012
  • 1
  • 39
  • 68
1

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){}
});
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
1

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;
    }
}
jrgermain
  • 11
  • 1
-4

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

AtashG79
  • 1
  • 1