PDA

View Full Version : Font Sizer Control (Functionality)


nyildizian
05-28-2009, 11:31 AM
I am using the following code in my Website/Web page to control the font size, i.e. to either increment or decrement it. The code works, however, when I first click on the increment button instead of increasing the font size it decreases it, but this happens ONLY on the FIRST initial click. Afterwards, it works as it should do, i.e. increases the font size. What I am doing wrong? Any assistance will be much appreciated.

[CODE] (in the JSHeader)

var min=8;
var max=18;

function increaseFontSize() {
var p = document.getElementsByTagName('p');
for(i=0; i<p.length; i++) {
if(p[i].style.fontSize) {
var s = parseInt(p[i].style.fontSize.replace("px",""));
} else {
var s = 12;
}
if(s!=max) {
s += 1;
}
p[i].style.fontSize = s+"px"
}
}

function decreaseFontSize() {
var p = document.getElementsByTagName('p');
for(i=0; i<p.length; i++) {
if(p[i].style.fontSize) {
var s = parseInt(p[i].style.fontSize.replace("px",""));
} else {
var s = 12;
}
if(s!=min) {
s -= 1;
}
p[i].style.fontSize = s+"px"
}
}

[CODE] (on the Web page)

<a href="javascript:decreaseFontSize();">-</a>
<a href="javascript:increaseFontSize();">+</a>

Thanks in advance.

mic2100
05-28-2009, 05:39 PM
does it do this because the initial size of the fonts is larger than wot u are changing them to?

v08i
05-29-2009, 07:40 AM
Here is the functionality that you want. Changin font size with javascript. (http://www.vijayjoshi.org/2009/04/02/changing-font-size-on-a-page-with-javascript-for-better-user-experience/)