color-scheme: dark light; and user enabled theme change with JS
Hi, I want to ask what the best way to do the following is:
1. I want to use ,
2. have JS recognize which scheme the user has enabled
3. based on which scheme the user has, a button would be used to change to the opposite theme.
4. I do not want to additionally define what dark/light theme would be like, like we would usually do without color-scheme in CSS.
so I have this code
ChatGPT says I should have additionally defined what dark/light theme would be like, like we would usually do without color-scheme in CSS - which I do not want.
so with JS , I would detect their scheme and change the theme, but now it is made by calling to "data-theme" (which I don't know what it is) and calling on the additionally CSS definitions of dark and light (which I do not want to use).
4 Replies
I recommend changing the listener into function and giving the button onclick attribute with the name of the function in HTML.
also change the setAttribute into "dataset.theme=currentTheme"
you kinda already have working code(atleast from what I see cause I can't test it because I am on mobile)
the thing is, I don't want to use additional CSS
I think I found the solution:
okay. BTW if you don't want to use the default browser color scheme, you will need to write something like this:
thank you!!