12

Is it possible to detect all style sheets on the current page with a click of a ‘disable/enable CSS’ button and disable them on the first click so none of the styling applies, and then restore them once again on second click? Any idea what the jQuery would look like, if it’s possible?

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Maverick
  • 1,123
  • 5
  • 16
  • 30

6 Answers6

25
$('link[rel="stylesheet"]').attr('disabled', 'disabled');

this should disable all of them, then the opposite to renable them:

$('link[rel="stylesheet"]').removeAttr('disabled');
MaxOvrdrv
  • 1,780
  • 17
  • 32
  • Short and sweet. Works great. Thanks MaxOvrdrv! – Maverick Apr 05 '13 at 00:49
  • @mdmullinax your fiddle doesn't seem to work on button click! – Maverick Apr 05 '13 at 00:51
  • @mdmullinax you're right works in chrome. Not in FF. Either way great answer! Upvoting your fiddle too :) – Maverick Apr 05 '13 at 00:55
  • 11
    This does not work in Firefox. [MDN says](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-disabled) that using `disabled` as an *HTML attribute* is **non-standard**; `disabled` is only a *DOM property*. You can fix your code by using `.prop('disabled', true)` and `.prop('disabled', false)` instead. – Rory O'Kane Sep 20 '13 at 17:29
  • @RoryO'Kane is correct. Better to follow his suggestion. – Fizzix May 28 '14 at 06:30
  • Definitively see @RoryO'Kane answer below for a much more compatible solution on various browsers http://stackoverflow.com/a/18922945/954777 – smonff Feb 03 '15 at 17:52
8

To disable all stylesheets:

$('link[rel~="stylesheet"]').prop('disabled', true);

To re-enable all stylesheets:

$('link[rel~="stylesheet"]').prop('disabled', false);

I use the ~= attribute selector here instead of = so that the selector will still work with stylesheets where the rel attribute is alternate stylesheet, not just stylesheet.

Also, it is important to use .prop, not .attr. If you use .attr, the code will not work in Firefox. This is because, according to MDN, disabled is a property of the HTMLLinkElement DOM object, but not an attribute of the link HTML element. Using disabled as an HTML attribute is nonstandard.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
2
$('link[rel=stylesheet][href~="somelink.com"]').attr('disabled', 'true');
What have you tried
  • 11,018
  • 4
  • 31
  • 45
1

Here is a rough example that relies on assigning an id to a style and then removing and restoring it using a variable.

HTML

<style id="test">
.test{
  background: red;
  width: 100px;
  height: 100px;
}
</style>
<div class="test">Something</div>
<div id="remove">Click to Remove</div>
<div id="restore">Click to Restore</div>

Javascript

var css = $("#test");
$("#remove").click(function(){
    css.remove();
});

$("#restore").click(function(){
    $("head").append(css);
});

Working Example http://jsfiddle.net/Fwhak/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

I found the found the following worked in all browsers for me:

CSS Link:
<link rel="stylesheet" href="http://basehold.it/22" data-role="baseline">

JQuery:
$('link[data-role="baseline"]').attr('href', '');

The above will disable only that one stylesheet, and this could be toggled on and off.

Alex
  • 2,651
  • 2
  • 25
  • 45
0

Here is another method that you can try to do that thing.

$('*[rel=stylesheet]').attr('disabled', 'true');
$('link[rel=stylesheet]').attr('disabled', 'true');

and in other hand you remove that attribute

$('link[rel=stylesheet]').attr('disabled', 'false');
$('*[rel=stylesheet]').attr('disabled', 'false');
Hassan Raza
  • 671
  • 10
  • 27