I have a code for font change in my application as below -
HTML -
<button onclick="incdec(1)"></button>
<button onclick="incdec(-1)"></button>
<p id="element1>Element 1 has font size 12px</p>
<p id="element2>Element 2 has font size 14px</p>
JS -
var items = [{
"el": "#element1",
"maxSize": 14,
"minSize": 12
}, {
"el": "#element2",
"maxSize": 16,
"minSize": 14
}]
var incdec = function(dir) {
items.forEach(function(item) {
size(dir, item)
});
};
var size = function(dir, item) {
var el = document.querySelector(item.el);
var max = item.maxSize;
var min = item.minSize;
console.log(dir, min, max)
var style = window.getComputedStyle(el, null).getPropertyValue('font-size');
var fontSize = parseFloat(style);
if (dir === 1 && fontSize < max) {
el.style.fontSize = (fontSize + 1) + 'px';
console.log("incSize", el.tagName, el.style.fontSize)
} else if (dir === -1 && fontSize > min) {
el.style.fontSize = (fontSize - 1) + 'px';
console.log("decSize", el.tagName, el.style.fontSize)
}
}
I want that the selected font size should remain on reload. How can I achieve this using local storage? Please note that the current(set/default) font for both the elements is different so different values have to be stored in the local storage for each. I need help with the logic.
Thanks!!!