3

I'm trying to use a dark theme and light theme on my webpage, but I can't find a way to toggle them with just one button (so 1st click of the button turns on the dark theme and 2nd time turns it off). I would prefer to be able to do this without the use of a third-party JavaScript library. I have found a way to do this with an <option> element but that isn't the "toggle button" I want:

  <button onclick=getTheme() id=themeToggle>Click to use this theme.</button>
    <select id="select">
        <option>Dark Theme</option>
        <option>Revert To Original</option>
    </select>

and

function getTheme () {
    function changeTheme (Theme) {
        document.getElementById('style').setAttribute('href', Theme);
    }
    var index = document.getElementById("select").selectedIndex;
    switch (index) {
        case 0:
          changeTheme('css/dark.css');
          break;
          case 1: changeTheme('css/main.css');
    }
}

Thanks for your help!

Wyatt Nulton
  • 129
  • 1
  • 2
  • 10
  • 2
    https://stackoverflow.com/questions/8796107/how-to-make-changeable-themes-using-css-and-javascript – Ajay Makwana Apr 11 '19 at 12:44
  • 5
    Possible duplicate of [How to make changeable themes using CSS and JavaScript](https://stackoverflow.com/questions/8796107/how-to-make-changeable-themes-using-css-and-javascript) – Rob Apr 11 '19 at 12:45
  • 1
    @Rob _"I have found a way to do this..."_ - TO is asking for a button to toggle between two states – Andreas Apr 11 '19 at 12:52
  • Use a closure, a global variable, check the value of the `href` attribute, ... – Andreas Apr 11 '19 at 12:55

3 Answers3

2

I know this seems a bit amateur but you can do something like this,

<button onclick=getTheme() id=themeToggle>Click to use this theme.</button>
<script>
var click = 0;
function getTheme()
{   
    click++;
    if(click%2 == 0)
        changeTheme('css/dark.css');
    else
        changeTheme('css/main.css');
}
</script>

and change text on button accordingly. Let me know if it works.

Komal R
  • 387
  • 2
  • 16
2

Another way is to use a boolean. You can change the value of the boolean on each click. And you can toggle the css with this boolean.

<button onclick=getTheme()>Click to use this theme.</button>

Js

var bool = true;
function getTheme() { 
  function changeTheme (theme) {
    document.getElementById('style').setAttribute('href', theme);
  }  

  bool = !bool;
  var theme = bool ? 'css/dark.css' : 'css/main.css';
  changeTheme(theme);
}

Edit based on comment

This does work perfectly, however, is there a way to use document.getElementById("themeToggle").innerHTML = "New text!"; with this to change the button between "dark theme" or "light theme"

You can use the same logic, like:

var bool = true;
function getTheme() { 
  function changeTheme (theme, text) {
    document.getElementById('style').setAttribute('href', theme);
    document.getElementById('themeToggle').innerText = text;
  }  

  bool = !bool;
  var theme = bool ? 'css/dark.css' : 'css/main.css';
  var text  = bool ? 'Dark theme' : 'Light theme';
  changeTheme(theme, text);
}
R3tep
  • 12,512
  • 10
  • 48
  • 75
0

A simple way is:

HTML

<input type="button" value="Dark" id="button" onclick=getTheme()></input> 

JavaScript

function getTheme () {
  var button = document.getElementById("button");
    if(button.value === "Dark") {
      button.value = "Revert To Original";
    } else {
      button.value = "Dark";
    }
}

Inside the if/else you can put your change theme function.

https://codepen.io/anon/pen/xedzVz?editors=1111