PS : I ended up creating two HTML buttons and using the script herewith (because any script I created relating to alternating the icons conflicted with your schemetoggle.js):
TWO BUTTONS
<!-- Dark mode toggle button (shown in light mode) -->
<button id="toggleToDarkMode" class="btn btn-dark" style="display: none;"><i
class="bi bi-moon-stars align-middle"></i></button>
<!-- Light mode toggle button (shown in dark mode) -->
<button id="toggleToLightMode" class="btn btn-warning" style="display: none;"><i
class="bi bi-brightness-high align-middle"></i></button>
FOOTER SCRIPT:
<script>
document.addEventListener('DOMContentLoaded', function() {
var toggleToDarkModeButton = document.getElementById('toggleToDarkMode');
var toggleToLightModeButton = document.getElementById('toggleToLightMode');
function updateButtonVisibility() {
if (document.body.classList.contains('dark')) {
toggleToDarkModeButton.style.display = 'none';
toggleToLightModeButton.style.display = '';
} else {
toggleToDarkModeButton.style.display = '';
toggleToLightModeButton.style.display = 'none';
}
}
// Check local storage for dark mode preference and apply it
if (localStorage.getItem('darkMode') === 'true') {
document.body.classList.add('dark');
} else {
document.body.classList.remove('dark');
}
updateButtonVisibility(); // Update the button visibility on load
toggleToDarkModeButton.addEventListener('click', function() {
document.body.classList.add('dark');
localStorage.setItem('darkMode', 'true');
updateButtonVisibility();
});
toggleToLightModeButton.addEventListener('click', function() {
document.body.classList.remove('dark');
localStorage.setItem('darkMode', 'false');
updateButtonVisibility();
});
});
</script>
Obviously it would be better if the data attribute you suggested worked, so please take a look - it would be a very useful feature and I am planning on using Canvas for many websites so I am interested in an unlimited license.
Kind regards,
Pete