We are using the following script to set the color theme on my page. When user clicks a button and it sets a class on the HTML. When the user clicks another button(s) then it sets that classname instead. This works okay.
<script type="text/javascript">
if (localStorage.buttonColor) {
document.getElementsByTagName('html')[0].className = localStorage.buttonColor
}
function getButtonColor(buttonName) {
localStorage.buttonColor = buttonName;
document.getElementsByTagName('html')[0].className = buttonName
}
</script>
If there's a localstorage value for the button color then it sets the class. However we also need it to disable the button. How can we make it disable the button based on the value of the localstorage?
Also if a user clicks on a button then we want that button to be disabled and all the others with the class "theme" to be enabled. We are looking for a javascript only solution.
Here's the HTML:
<form class="ng-pristine ng-valid">
<button class="theme" name="darkBlue" onclick="getButtonColor(this.name)">Blue</button>
<button class="theme" name="black" onclick="getButtonColor(this.name)">Black</button>
</form>
Here's one solution but it's not quite all that's needed as we have now decided to give the buttons a class name to make it easier to select them. Also this only works for getting from local storage and we still need to disable a button if a user clicks it and enable all the others.
if (localStorage.buttonColor) {
document.getElementsByTagName('html')[0].className = localStorage.buttonColor
var buttons = document.body.getElementsByTagName('form')[0].getElementsByTagName('button')
for(var i =0; i < buttons.length; i++)
if(buttons[i].name == localStorage.buttonColor) button.disabled = true
}