I think it would be more organized to have CSS classes for those sources like this:
.serif {
font-family: Times, "Times New Roman", Georgia, serif;
}
.sansserif {
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.monospace {
font-family: "Lucida Console", Courier, monospace;
}
.cursive {
font-family: cursive;
}
.fantasy {
font-family: fantasy;
}
and then with Javascript add or remove the class. It could be something like this:
var textarea = document.getElementById('text');
var select = document.querySelector('select');
select.addEventListener('change', function() {
[].forEach.call(select.children, function(opt, i) {
textarea.classList.toggle(opt.value, i == select.selectedIndex);
});
});
This code traverses all options and using the .classList.toggle()
sets or removes the class according to the index of the option to be iterated is the same as select.selectedIndex
, or is the option chosen.
This from the presuposto that you give each option
the value
corresponding to each class. For example: <option value="fantasy">Fantasy</option>
Thanks in the case text being the textarea is pulled by the element I can confirm a source for it and then it changes? Example textarea { font-family: Arial; }
– Hector Gabriel Garcia