0

The purpose is to make a custom site color theme. The problem is when I changing the second CSS variable first resets to its default value.

enter image description here

enter image description here

var mainColor = document.getElementById("main-color");
var secondColor = document.getElementById("second-color");

function color(cssVariable, inputValue) {
  return cssVariable + ":" + inputValue
}

mainColor.addEventListener('input', function() {
  document.documentElement.setAttribute("style", color("--main-color", this.value))
})

secondColor.addEventListener('input', function() {
  document.documentElement.setAttribute("style", color("--second-color", this.value))
})
:root {
  --main-color: black;
  --second-color: whitesmoke;
  --main-font-size: 16px;
  color: var(--main-color);
  font-size: var(--main-font-size);
}
<div class="dropdown-color-container">
  <input type="color" id="main-color">
  <input type="color" id="second-color">
</div>

Is there any way to save changed CSS variables? And how would you implement code for this, what's the easiest solution? I'm beginner in JS

Runnable code: https://www.w3schools.com/code/tryit.asp?filename=GLDQWOUT9HUA

4
  • your code seems to work fine Commented Dec 5, 2020 at 22:24
  • 1
    @TemaniAfif gif added Commented Dec 5, 2020 at 22:30
  • we don't need gif, we need a runnable code that highlight the issue Commented Dec 5, 2020 at 22:34
  • 1
    @TemaniAfif check picture too, I think there is very helpful info. If no clue I'll add runnable code Commented Dec 5, 2020 at 22:38

2 Answers 2

1

The issue is that you are setting the style attribute so each time you override all the previous inline style.

You need to do it differently using setProperty

var mainColor = document.getElementById("main-color");
var secondColor = document.getElementById("second-color");

function color(cssVariable, inputValue) {
  return cssVariable + ":" + inputValue
}

mainColor.addEventListener('input', function() {
  document.documentElement.style.setProperty("--main-color", this.value);
})

secondColor.addEventListener('input', function() {
  document.documentElement.style.setProperty("--second-color", this.value)
})
:root {
  --main-color: black;
  --second-color: whitesmoke;
  --main-font-size: 16px;
  color: var(--main-color);
  font-size: var(--main-font-size);
}
<div class="dropdown-color-container">
  <input type="color" id="main-color">
  <input type="color" id="second-color">
</div>

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

1 Comment

Didn't know about that. Thank you!
1

Change this line
document.documentElement.setAttribute("style", color("--main-color", this.value));

to

document.documentElement.style.setProperty("--main-color", this.value);

Refer:
https://stackoverflow.com/questions/37801882/how-to-change-css-root-color-variables-in-javascript

Demo: jsFiddle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.