How to switch to dark mode on click event in Javascript

I’m building a web page and I am trying to toggle the prefers-color-scheme on a click event. That is, when the button is clicked I wish that the windows detect the media query @media (prefers-color-scheme:dark)

theme.addEventListener("click", () => {
   // event to switch theme
}

Is this possible?