-1

I want to implement a dark mode in my website. I have a switch with a checkbox input and I want to run a javascript function when it is checked so that the colors change. Here is the html code. I don't know if the CSS code is needed for this.

<label class="switch">
<input type="checkbox" id="darkmode">
<span class="slider round"></span>
</label>
Linder Leiva
  • 105
  • 1
  • 8

2 Answers2

1
var checkbox = document.getElementById("darkmode");

checkbox.addEventListener( 'change', function() {
    if(this.checked) {
        // Enable dark mode
    } else {
        // Disable dark mode
    }
});
jcoleau
  • 1,112
  • 7
  • 20
0

function check() {
 var check=   document.getElementById("darkmode").checked;
 
 console.log(check);
}
<label class="switch">
<input type="checkbox" id="darkmode" >
<span class="slider round"></span>
<button onclick='check()'>press</button>
</label>
DCR
  • 14,737
  • 12
  • 52
  • 115