Skip to content
Advertisement

How to change toggle to icon click (for switching to dark mode)

I’ve dark mode enabled on the website. It currently has a toggle switch, which changes the layout from light to dark and vice versa.

It is using the following code:

JavaScript
JavaScript

Now I want to upgrade to an icon click. For example, if there is light mode enabled, the icon for dark mode should be seen upon clicking it, the user will get dark mode on. Same, if dark mode is enabled, the icon for the light mode to be displayed, and if the user clicks it, the light mode is activated.

Thanks for any help or suggestions.

Advertisement

Answer

in the HTMl make a new image as a lable:

JavaScript

then you can use the set attribute to switch the source

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement