4

I was wondering, is it possible to change the attached stylesheet using JavaScript?

For example:

<link href="stylesheet.css" rel="stylesheet" type="text/css">
.....
<div id="controls">
    <div id="red" onClick="styler(1)">R</div>
    <div id="green" onClick="styler(2)">G</div>
    <div id="blue" onClick="styler(3)">B</div>
    <div id="reset" onClick="styler(4)">Reset</div>
</div>

JavaScript

function styler(attr){
    switch(attr){
         case'1':document.----- = "stylesheet1.css";break;
         case'2':document.----- = "stylesheet2.css";break;
         case'3':document.----- = "stylesheet3.css";break;
         case'4':document.----- = "stylesheet.css";break;
         default:;break;
     }
}
MCMXCII
  • 1,043
  • 4
  • 13
  • 26
  • There is an API that allows you to modify lines in stylesheets, but you really don't want to. CSS is expressive enough. – Halcyon Jun 06 '14 at 16:52

4 Answers4

7

Add an id to the link tag and use

<link id="myStyleSheet" href="stylesheet.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
    function styler(attr){
        var href;
        switch(attr){
            case'1':href = "stylesheet1.css";break;
            case'2':href = "stylesheet2.css";break;
            case'3':href = "stylesheet3.css";break;
            case'4':href = "stylesheet.css";break;
            default:;break;
        }
        document.getElementById('myStyleSheet').href = href;
    }
</script>

See this post

Community
  • 1
  • 1
Preston S
  • 2,751
  • 24
  • 37
0

It should be easy:

<html>
<head>
    <link rel="stylesheet" href="/tmp/default.css" id="default">
</head>

<body>
<button id="bt">change style</button>

<script>
    var targetStyle = document.querySelector('#default'),
        otherStyle = '/tmp/style2.css';

    document.querySelector('#bt').onclick = function(){
        targetStyle.href=otherStyle;        
    };

</script>

</body>
</html>
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
  • 1
    Don't use querySelector when only selecting by id, its much slower than getElementById. [jsPerf test](http://jsperf.com/getelementbyid-vs-queryselector) – Preston S Jun 06 '14 at 17:14
  • @PrestonS It depends! Moreover this is just an example, it can fetch element nodes through their own methods. Frankly, downvote so it's just sick! – felipsmartins Jun 06 '14 at 17:18
0

Try it

 <!DOCTYPE html>
    <html>
    <head>
    <link id="pagestyle" rel="stylesheet" type="text/css" href="default.css">
    <script>
    function swapStyleSheet(sheet){
        document.getElementById('pagestyle').setAttribute('href', sheet);
    }
    </script>
    </head>
    <body>
    <h2>Javascript Change StyleSheet Without Page Reload</h2>
    <button onclick="swapStyleSheet('dark.css')">Dark Style Sheet</button>
    <button onclick="swapStyleSheet('blue.css')">Blue Style Sheet</button>
    <button onclick="swapStyleSheet('default.css')">Default Style Sheet</button>
    </body>
    </html>
Mohit maru
  • 817
  • 8
  • 15
0

Another option is to toggle the disabled property of the link DOM element. According to Mozilla the disabled HTML attribute is non-standard, but the javascript property is. So if you're toggling it with JS you should be fine.

emersonthis
  • 32,822
  • 59
  • 210
  • 375