After spending an entire afternoon on my JavaScript issue as a beginner, I have a specific goal in mind: allowing the user to select and change the main color of the page.
To implement this, I inserted a color picker in my HTML:
<input type="color" id="colorID" oninput="changeColor()">
I then found this code online:
// When input changes, store value as 'storedValue'
function changeColor() {
var userColor = document.getElementById('colorID').value;
localStorage.setItem('storedValue', document.body.style.backgroundColor = userColor);
}
// If there is a stored value, update color picker and background color
if(localStorage.storedValue) {
document.getElementById('colorID').value = localStorage.storedValue;
document.body.style.backgroundColor = localStorage.storedValue;
}
My aim was to replace "document.body.style.backgroundColor" with my --main-color: #fff; from :root. Despite trying various alternatives like using document.documentElement, nothing seemed to do the trick!