Created
May 7, 2022 16:08
-
-
Save wrapcoders/2ebfef732412dac4b75cabeeabca43aa to your computer and use it in GitHub Desktop.
RwWpEWy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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