, . jsfiddle: http://jsfiddle.net/WxKJs/7/
I made a FontSize class that stores the local font size value sent to the constructor. I gave him a method setthat will allow you to override and set a new font size if the value is passed as a parameter. As a security check, you probably want to check that your font size is int before you try to save and set it.
function FontSize(size) {
var self = this;
function isInteger(value) {
return (Object.prototype.toString.call(value) === '[object Integer]');
}
function isNumber(value) {
return (Object.prototype.toString.call(value) === '[object Number]');
}
function isMouseEvent(value) {
return (Object.prototype.toString.call(value) === '[object MouseEvent]');
}
function getInt(value) {
var val = parseInt(value, 10);
if (isNaN(val)) return 12;
if (isInteger(val)) return val;
if (isNumber(val)) return Math.round(val);
return null;
}
self.fontSize = getInt(size);
self.set = function (override) {
var fontSize = self.fontSize;
if (override && !isMouseEvent(override)) {
fontSize = getInt(override);
}
console.log("font size:", self.fontSize, ", override font size:", fontSize);
document.body.style.fontSize = fontSize + 'px';
};
}
function setFontSizeInput(ipval) {
console.log("font size ipval : "+ipval);
var inputFontSize = new FontSize(ipval);
inputFontSize.set();
}
var fontsize18 = new FontSize(18);
var fontsize14 = new FontSize(14);
var fontsize16 = new FontSize(16);
document.getElementById('size-18').onclick = fontsize18.set;
document.getElementById('size-14').onclick = fontsize14.set;
document.getElementById('size-16').onclick = fontsize16.set;
source
share