- Don't use
for..in
to loop through a node list. Use afor
loop instead. - You need to get the current font size from the element, you can use
window.getComputedStyle()
for this. - You can easily shorten your code to one function and leave the scale of font size down to a parameter.
How is the following:
function fontSize(num) {
var alineas = document.getElementsByTagName('p');
for (i=0; i<alineas.length; i++) {
var el = alineas[i]
var oSize = window.getComputedStyle(el, null).getPropertyValue('font-size');
var nSize = Number(oSize.replace("px",""))+num;
el.style.fontSize = nSize+"px"
}
}
Call it like fontSize(5)
or fontSize(-5)
to go up 5 pixels or down 5 pixels respectively.