1

I would like to set the color scheme based on simple CSS root variables. The JavaScript does not work: it does not see/set the root variables when one of the options are clicked. What have I overlooked to make this simple color scheme work?

const setTheme = theme => document.documentElement.className = theme;
document.getElementById('themeoptions').addEventListener('change', function() {
    setTheme(this.value);
});
#themeoptions p{ /* User Interface */
  display: inline-block;
  text-decoration: underline;
}#themeoptions p:hover{cursor: pointer}


:root.light {
  --bgr: #ddc;
  --txt: #456;
}
:root.dark {
  --bgr: #222;
  --txt: #844;
}
:root.blue {
  --bgr: #046;
  --txt: #dde;
}


body {
  background-color: var(--bgr);
  color: var(--txt);
}
<div id="themeoptions">
    <p value="light">Light</p>
    <p value="dark">Dark</p>
    <p value="blue">Blue</p>
</div>

<h1>Click on a theme to change the color scheme!</h1>

2
  • What change event do you think gets triggered here? Commented May 27, 2022 at 22:22
  • Should 'change' be 'click' ? But then still it does not work. This is a tough one for me. Commented May 27, 2022 at 22:32

1 Answer 1

4

There are three problems to address in your JavaScript code:

  1. The value property on an element does not refer to the attribute that you have named value on your paragraph elements. To access the value of this property, you'll need to use Element.getAttribute().

  2. this in your event listener callback function does not refer to the target element of the event. To access the target element, you'll need to use Event.target.

  3. The event that you want to listen for is most likely the click event (not the change event).

const setTheme = theme => document.documentElement.className = theme;
document.getElementById('themeoptions').addEventListener('click', ({target}) => {
  setTheme(target.getAttribute('value'));
});
#themeoptions p{ /* User Interface */
  display: inline-block;
  text-decoration: underline;
}#themeoptions p:hover{cursor: pointer}


:root.light {
  --bgr: #ddc;
  --txt: #456;
}
:root.dark {
  --bgr: #222;
  --txt: #844;
}
:root.blue {
  --bgr: #046;
  --txt: #dde;
}


body {
  background-color: var(--bgr);
  color: var(--txt);
}
<div id="themeoptions">
    <p value="light">Light</p>
    <p value="dark">Dark</p>
    <p value="blue">Blue</p>
</div>

<h1>Click on a theme to change the color scheme!</h1>

Sign up to request clarification or add additional context in comments.

10 Comments

Awesome! Thanks for the clarity of the three problems. I will now investigate them one by one. And thank you for the working solution!
I also figured that by adding :root, in the beginning of the desired default color scheme, that scheme will be loaded by default, if no scheme is set yet.
I wonder, would it be possible to have a theme called :root.default {} in the CSS, and have the Javascript code to load the "default" theme if nothing is set yet?
@Såm You are always welcome to ask a new question if you have one.
Roger that, I will :)
|

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.