Answer the question
In order to leave comments, you need to log in
How to store button clicks in localstorage?
How to save button click in local storage or cookie
here is html
<div class="theme-kn">
<i class="fa-solid fa-lightbulb-on theme-i getdark" aria-hidden="true"></i><i class="fa-solid fa-lightbulb-slash theme-i getlight"></i>
</div>
$('.getlight').click(function () {
$('link[href="templates/gor/css/light.css"]').attr('href', 'templates/gor/css/dark.css');
});
$('.getdark').click(function () {
$('link[href="templates/gor/css/dark.css"]').attr('href', 'templates/gor/css/light.css');
});
Answer the question
In order to leave comments, you need to log in
First, set an identifier to the tag with the style connection
<link id="theme" href="templates/gor/css/light.css">
function setTheme(theme) {
document.getElementById('theme').setAttribute('href', `templates/gor/css/${theme}.css`);
}
$('.getlight').on('click', function () {
setTheme('light');
localStorage.setItem('selectedTheme', 'light'); // Запомнить
});
$('.getdark').on('click', function () {
setTheme('dark');
localStorage.setItem('selectedTheme', 'dark'); // Запомнить
});
const selectedTheme = localStorage.getItem('selectedTheme') ?? 'light';
setTheme(selectedTheme);
Look Away: Create A Dark/Light Mode Switch with CSS Variables
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question