Skip to content

Instantly share code, notes, and snippets.

@wrapcoders
Created May 7, 2022 16:08
Show Gist options
  • Save wrapcoders/2ebfef732412dac4b75cabeeabca43aa to your computer and use it in GitHub Desktop.
Save wrapcoders/2ebfef732412dac4b75cabeeabca43aa to your computer and use it in GitHub Desktop.
RwWpEWy
The font is: <select class="option" id="test" name="test">
<option value="arial">Arial</option>
<option value="uppercase">Uppercase</option>
<option value="comicsans">Comic sans (dyslexic friendly)</option>
</select>
//Await the DOM to be loaded - just like window.onload, but triggers faster
document.addEventListener('DOMContentLoaded', ()=>{
//Note that it's change, not onchange:
document.getElementById("test").addEventListener('change', selectFonts)
})
//I've simplified you code a bit...
function selectFonts(){
const element = document.getElementById("test")
const selected = element.options[element.selectedIndex].value;
//We do the same things for each value - so, let's simplify:
//Remove all classes
for(const font of ['arial', 'uppercase', 'comicsans'])
document.body.classList.remove(font)
//Add the active class
document.body.classList.add(selected)
}
document.addEventListener('DOMContentLoaded', ()=>{
document.getElementById("test").addEventListener('change', function() {
const element = document.getElementById("test")
const selected = element.options[element.selectedIndex].value;
window.localStorage.setItem('test', selected)
})
const selected = window.localStorage.getItem('test')
if(selected){
for(const elem of document.getElementById("test").options){
if(selected === elem.value){
elem.selected = true
break
}
}
document.body.classList.add(selected)
}
})
body{font-family:arial}
body.uppercase {text-transform:uppercase}
body.comicsans {font-family:comic sans ms}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment