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!

4
  • 2
    stackoverflow.com/questions/8796107/… Commented Apr 11, 2019 at 12:44
  • 5
    Possible duplicate of How to make changeable themes using CSS and JavaScript Commented Apr 11, 2019 at 12:45
  • 1
    @Rob "I have found a way to do this..." - TO is asking for a button to toggle between two states Commented Apr 11, 2019 at 12:52
  • Use a closure, a global variable, check the value of the href attribute, ... Commented Apr 11, 2019 at 12:55

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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"
Wow nice answer
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.