Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Change css file with js

<!DOCTYPE html>
<html>
  <head>
    <title>Changing CSS extern file using only JavaScript</title>
    <link id="changeCSS" rel="stylesheet" type="text/css" href="positive.css"/>
    <script type="text/javascript">
      function changeCSS() {

        var oldlink = document.getElementById("changeCSS");
        var cssFile;

        if(oldlink.getAttribute('href') === 'positive.css') {
          cssFile = 'negative.css';
        }
        else {
          cssFile = 'positive.css';
        }

        var newlink = document.createElement("link");
        newlink.setAttribute("id", "changeCSS");
        newlink.setAttribute("rel", "stylesheet");
        newlink.setAttribute("type", "text/css");
        newlink.setAttribute("href", cssFile);

        document.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);
      }
    </script>
  </head>
  <body>
    <button onclick="changeCSS();">change</button>
    <div class="box">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</div>
  </body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: Substring in Javascript using slice 
Javascript :: react hook form submit outside form 
Javascript :: Setting up an Express project 
Javascript :: string filter javascript 
Javascript :: datatable buttons bootstrap 4 
Javascript :: latex sum two lines subscript 
Javascript :: mongoose encrypt database using mongoose encryption package 
Javascript :: associative multidimensional array javascript 
Javascript :: get data firebase 
Javascript :: shadow generator react native 
Javascript :: add pdf in react app 
Javascript :: node is not recognized as internal command 
Javascript :: angular convert boolean to yes no 
Javascript :: ionic vue use .env 
Javascript :: object declaration in javascript 
Javascript :: adding all elements in an array javascript 
Javascript :: sequelize exclude attributes 
Javascript :: node express 
Javascript :: automated counter with react hooks 
Javascript :: json date serialize 
Javascript :: pass object in asyncstorage in react native 
Javascript :: ajax 
Javascript :: react function runs several times 
Javascript :: change element text innerhtml keeping the elements or tags inside 
Javascript :: jquery limit words in string 
Javascript :: JavaScript Creating Symbol 
Javascript :: react-multi-carousel infinite loop causing issue 
Javascript :: form serialze submit button 
Javascript :: react-with-firebase-auth 
Javascript :: expressjs param 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =