0

I want to change theme color settings using this code, but it is not working on Firefox. The source code that I used is from this website: https://jonathan-harrell.com/experiment/live-theming-with-css-variables/. Here is my code:

const setValue = (property, value) => {
    if (value) {
        document.documentElement.style.setProperty(`--${property}`, value);

        const input = document.querySelector(`#${property}`);
        if (input) {
            value = value.replace('px', '');
            input.value = value;
        }
    }
};

const setValueFromLocalStorage = property => {
    let value = localStorage.getItem(property);
    setValue(property, value);
};

const setTheme = options => {
    for (let option of Object.keys(options)) {
        const property = option;
        const value = options[option];

        setValue(property, value);
        localStorage.setItem(property, value);
    }
}

const dataThemeButtons = document.querySelectorAll('[data-theme]');

for (let i = 0; i < dataThemeButtons.length; i++) {
    dataThemeButtons[i].addEventListener('click', () => {
        const theme = dataThemeButtons[i].dataset.theme;
        switch (theme) {
            case 'default':
                setTheme({
                    'meta-menu-background-color': '#50463d',
                    'link-color': '#ed1347',
                    'header-menu-color': '#50463d',
                    'header-menu-background-color': '#ffffff',
                    'footer-background-color': '#3c332e',
                    'footer-last-background-color': '#50463d',
                });
            return;
        }
    })
}

document.addEventListener('DOMContentLoaded', () => {
    setValueFromLocalStorage('meta-menu-background-color'),
    setValueFromLocalStorage('link-color'),
    setValueFromLocalStorage('header-menu-color'),
    setValueFromLocalStorage('footer-background-color');
    setValueFromLocalStorage('footer-last-background-color');
});

const handleInputChange = (property, pixels) => {
    document.documentElement.style.setProperty(`--${property}`, `${event.target.value}${pixels ? 'px' : ''}`);
    localStorage.setItem(property, `${event.target.value}${pixels ? 'px' : ''}`);
};

document.querySelector('#meta-menu-background-color').addEventListener('change', event => {
    handleInputChange('meta-menu-background-color', false);
});

In Firefox console I see the error "ReferenceError: event is not defined" on this line:

document.documentElement.style.setProperty(`--${property}`, `${event.target.value}${pixels ? 'px' : ''}`);

It is working fine on Chrome.

Could you help me with this?

1
  • You're not passing event to do the function so that's why it's undefined.
    – Dehli
    Commented Oct 31, 2017 at 13:31

1 Answer 1

1

The event symbol is global in IE and Chrome, but it's passed as a parameter in Firefox. Thus you need to write your event handler functions with a single parameter (call it event if you want) and then add a preamble line of code to each handler:

function yourHandler(event) {
    event = event || window.event;
    // ...
}
2
  • Could you help me how to use that in my example? I am newbie in JavaScript.
    – marek8623
    Commented Oct 31, 2017 at 14:01
  • @MarekKozela you'll have to pass event as an additional parameter to handleInputChange(). The event handler that calls it already has an event parameter. That line in my code above would be added before the updated call to handleInputChange().
    – Pointy
    Commented Oct 31, 2017 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.