ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

Dark Light Mode website, jQuery

jQuery code for dark light mode website

$(".darkBTN").click(function(){
		localStorage.setItem('darkModeSet', 'darkModeNow');
		$("body").removeClass("light_Mode");
		$("body").addClass("dark_Mode");
});

$(".lightBTN").click(function(){
		localStorage.removeItem("darkModeSet");
		$("body").removeClass("dark_Mode");
		$("body").addClass("light_Mode");
});


if(localStorage.getItem('darkModeSet') == 'darkModeNow') {
		$("body").removeClass("light_Mode");
		$("body").addClass("dark_Mode");
} 

 

 

CSS code for dark light mode website

.lightBTN, .darkBTN{ padding:10px; display:none;}
.darkBTN{ background-color:black; color:white;}
.lightBTN{ background-color:white; color:black;}
.light_Mode{ background-color: white;color:black;}
.dark_Mode{ background-color:black; color:white;}
.light_Mode .lightBTN{ display:none;}
.light_Mode .darkBTN{ display:block;}
.dark_Mode .lightBTN{ display:block;}
.dark_Mode .darkBTN{ display:none;}
.alwaysRed{ color:red;}

 

 

Complete code for dark light mode website

<!DOCTYPE html>
<html>
<head>

<style>
		.lightBTN,.darkBTN{ padding:10px; display:none;}
		.darkBTN{ background-color:black; color:white;}
		.lightBTN{ background-color:white; color:black;}
		.light_Mode{ background-color: white;color:black;}
		.dark_Mode{ background-color:black; color:white;}
		.light_Mode .lightBTN{ display:none;}
		.light_Mode .darkBTN{ display:block;}
		.dark_Mode .lightBTN{ display:block;}
		.dark_Mode .darkBTN{ display:none;}
		.alwaysRed{ color:red;}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
		$(".darkBTN").click(function(){
				localStorage.setItem('darkModeSet', 'darkModeNow');
				$("body").removeClass("light_Mode");
				$("body").addClass("dark_Mode");
		});
		
		$(".lightBTN").click(function(){
				localStorage.removeItem("darkModeSet");
				$("body").removeClass("dark_Mode");
				$("body").addClass("light_Mode");
		});
		
		if(localStorage.getItem('darkModeSet') == 'darkModeNow') {
				$("body").removeClass("light_Mode");
				$("body").addClass("dark_Mode");
		} 
});
</script>
</head>
<body class="light_Mode">
		
		<p><button class="lightBTN">Light Mode</button><button class="darkBTN">Dark Mode</button></p>
		<h1>Web title</h1>
		<h3 class="alwaysRed">The color of this text will not changing because set by own selector class ( alwaysRed ).   </h3>
		<h3>When the BODY class changes this TEXT will changes as well accorging to the properties of the <b>light_Mode </b> and <b>dark_Mode </b> classes.</h3>
		
</body>
</html>
Categroy:
Front-end , JQuery