I have a system to change the layout of the page for a night vision layout, but for that to happen I had to use two buttons, and I think it's better to use a toggle button to change the theme. If you can help I thank you.
function applyTheme (theme) {
"use strict"
document.getElementById("mypage").className = theme;
localStorage.setItem ("theme", theme);
}
function applyDayTheme () {
"use strict"
applyTheme("day");
}
function applyNightTheme() {
"use strict"
applyTheme("night");
}
function addButtonLestenrs () {
"use strict"
document.getElementById("b1").addEventListener("click", applyDayTheme);
document.getElementById("b2").addEventListener("click", applyNightTheme);
}
function initiate(){
"use strict"
if(typeof(localStorage)===undefined)
alert("the application can not be executed properly in this browser");
else{
if(localStorage.getItem("theme")===null)
applyDayTheme();
else
applyTheme(localStorage.getItem("theme"));
}
addButtonLestenrs();
}
initiate();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="Estilos.css">
</head>
<body id="mypage" >
<h1>Choose a theme</h1>
<button id="b1">Theme day</button>
<button id="b2">Theme night</button>
<p> This is an example of use of HTML5 storage API </p>
<div class="teste1" >
in
</div>
<script type="text/javascript" src="change-theme.js"></script>
</body>
</html>
.day{
color:black;
background-color:rebeccapurple;
}
.night{
color:white;
background-color:black;
}