35 lines
No EOL
1.3 KiB
JavaScript
35 lines
No EOL
1.3 KiB
JavaScript
// Radio button state management with localStorage
|
|
function initRadioState() {
|
|
// Store and retrieve radio button state
|
|
const radioButtons = document.querySelectorAll('input[name="mode"]');
|
|
|
|
// Load saved state on page load
|
|
const savedMode = localStorage.getItem('selectedMode');
|
|
if (savedMode) {
|
|
const radioToCheck = document.getElementById(savedMode);
|
|
if (radioToCheck) {
|
|
radioToCheck.checked = true;
|
|
// Set the data attribute to match the saved mode
|
|
document.documentElement.setAttribute('data-website-mode', savedMode);
|
|
}
|
|
} else {
|
|
// If no saved state, set to the currently checked radio button
|
|
const checkedRadio = document.querySelector('input[name="mode"]:checked');
|
|
if (checkedRadio) {
|
|
document.documentElement.setAttribute('data-website-mode', checkedRadio.id);
|
|
}
|
|
}
|
|
|
|
// Save state when radio button changes
|
|
radioButtons.forEach(radio => {
|
|
radio.addEventListener('change', function() {
|
|
if (this.checked) {
|
|
localStorage.setItem('selectedMode', this.id);
|
|
// Update the data attribute when mode changes
|
|
document.documentElement.setAttribute('data-website-mode', this.id);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export { initRadioState };
|