DEV Community

How to make a dark mode toggle with CSS and Javascript

Lens on December 10, 2022

Having a dark mode on your website is a great thing that many web developers use. It's also really easy to make with one CSS class and a javascript...
Collapse
 
emmancodex profile image
emmancodex

its not working in ...

im so fustrated right now😢`

button.addEventListener(click,
function () {
return document.body.classList.toggle(".darktheme");
});
`

`
`

Collapse
 
lensco825 profile image
Lens

@emmancodex I don't think you need the return keyword in your function since it only returns values like a string, number, or variable. Also you don't need the period on your class name because you just need the name and not the CSS selector. So it should be like this:

button.addEventListener('click', function () {
    document.body.classList.toggle("darkTheme");
  });
Enter fullscreen mode Exit fullscreen mode