index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DOM</title>
<style>
.dark {
background-color: rgb(0, 0, 0);
color: white;
}
.theme {
margin: 10px;
padding: 10px;
border: 0;
border-radius: 10px;
background-color: rgb(139, 48, 48);
color: white;
font-size: 16px;
text-transform: uppercase;
font-weight: bold;
cursor: pointer;
}
</style>
</head>
<body>
<div class="wrapper">
<button class="theme" id="theme">Dark Mode</button>
</div>
<script src="js.js"></script>
</body>
</html>
js.js
const btn = document.getElementById("theme");
function changeBg(color, text) {
document.body.style.backgroundColor = color;
btn.innerText = text;
}
//let currentTheme;
// btn.addEventListener("click", (e) => {
// currentTheme = currentTheme === "dark" ? "light" : "dark";
// console.log(e);
// // console.log(`changed theme chnage to theme ${currentTheme} `);
// if (currentTheme == "light") {
// changeBg("black", "LIGHT MODE");
// } else {
// changeBg('white', 'DARK MODE')
// }
// });
let currentTheme;
btn.addEventListener("click", (e) => {
if (!currentTheme || currentTheme == "dark") {
currentTheme = "light";
changeBg("black", "LIGHT MODE");
} else {
currentTheme = "dark";
changeBg('white', 'DARK MODE')
}
console.log(currentTheme);
});
Comments
Post a Comment