I have successfully created a CSS element to invert the entire website for a dark mode effect. I was able to use a key code to toggle the CSS element and it worked perfectly. However, my challenge now is that I want to implement a button to toggle the element.
CSS >
.dark-mode{
filter: invert(1) hue-rotate(180deg)
}
.invert{
filter: invert(1) hue-rotate(180deg)
}
Javascript >
document.onkeypress = function (e) {
e = e || window.event;
if(e.keyCode === 13){
document.documentElement.classList.toggle('dark-mode');
document.querySelectorAll('.inverted').forEach((result) => {
result.classList.toggle('invert');
})
}
}
While these codes work well, I am looking for a way to incorporate a button to toggle it instead.