Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67527

CHange style sheet with JS [duplicate]

$
0
0

This question already has an answer here:

I would like a script in JavaScript to change the active style sheet at the click of a button. My code is as follows:

index.html file :

<head>
    <script type="application/javascript" src="javascript.js">
    <link id="pagestyle" href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <button id="stylesheet1" onclick="initate();"> Default Style Sheet </button>
    <button id="stylesheet2" onclick='onclick="initate();"'> Dark Style Sheet </button>
</body>

javascript.js file :

 function swapStyleSheet(sheet) {
        document.getElementById("pagestyle").setAttribute("href", sheet);  
    }

    function initate() {
        var style1 = document.getElementById("stylesheet1");
        var style2 = document.getElementById("stylesheet2");

        style1.onclick = swapStyleSheet("default.css");
        style2.onclick = swapStyleSheet("dark.css");
    }

default.css :

   body{
        background-color: white;
        color: black
    }

and, at last, dark.css :

    body{
        background-color: black;
        color: white
    }

but it doesn't work. Can you help me? Thank you.


Viewing all articles
Browse latest Browse all 67527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>