tags:

views:

411

answers:

2

Hello,

Does anybody know how to modify the below script so that, in addition to the two current functions (changeFontSize( id, true); //Increases the font size - changeFontSize( id ); // Decreases size), I can add a third function whereby the font size will be reset to a default setting?

Thank you, Chris

SCRIPT

function changeFontSize( objId, doIncreaseSize ) { //Define your variables before use, or they will become global. var currentSize = 0, obj = document.getElementById( objId ), newVal = 0, limitMax = 10, limitMin = 0.5, unit = 0.1;

if( !obj ){
 return false; //Avoids errors if obj isn't found.
}
currentSize = parseFloat( obj.style.fontSize );
if( doIncreaseSize ){
 unit = -unit; // unit becomes negative. This turns subtractions into addition.
}
newVal = currentSize - unit;
if( limitMax >= newVal && limitMin <= newVal ){
 obj.style.fontSize = newVal + "em";
}

};

A: 

You will need to save the original font size (assuming it is not 1em) somewhere, like on the object itself or in an external array.

Something like this:

function changeFontSize(objId, doIncreaseSize) { 
  var currentSize = 0, obj = document.getElementById(objId), newVal = 0, limitMax = 10, limitMin = 0.5, unit = 0.1;
  if(!obj){
    return false;
  }
  currentSize = parseFloat( obj.style.fontSize );
  if (!obj.originalSize) { obj.originalSize = currentSize; }
  if(doIncreaseSize){
    unit = -unit;
  }
  newVal = currentSize - unit;
  if(limitMax >= newVal && limitMin <= newVal){
    obj.style.fontSize = newVal + "em";
  }
  return true;
}

function resetDefaultSize(objId) { 
  var obj = document.getElementById(objId);
  if(!obj){
   return false;
  }
  if (obj.originalSize) { obj.style.fontSize = obj.originalSize + "em"; }
  return true;
}

Notice your code will only work if the element has an explicitly defined font-size with units of em.

Sinan Taifour
Hello Mr. STAii,The script does not seem to work. When I click on INCREASE, DECREASE or RESET (see the following html code: CHANGE FONT SIZE: <a href="javascript:changeFontSize('mydiv', true);">INCREASE</a> | <a href="javascript:changeFontSize('mydiv');">DECREASE</a> | <a href="javascript:resetDefaultSize('mydiv');">RESET</a>) I get taken to a white screen with the word "true" on it. If you would be so kind as to please look at the javascript when you have a chance I would be most grateful. Thank you for your time. Chris
Chris
If you want a link that calls some javascript code, don't use `<a href="javascript:function();"></a>` rather use `<a href="#" onclick="function(); return false;">link</a>`. So in your case, that would be: `<a href="#" onclick="changeFontSize('mydiv', true); return false;">increase</a>`. Do the same for the other links.
Sinan Taifour
Hello Mr. STAii, it works!!!!!!! Thank you - you are the BOMB!!!!!!!!!!! Do you know how I could get each page to retain the increased/decreased font size of the previous page? Currently, when a visitor adjusts the font size and goes to another page the font size returns to the default size, meaning that the visitor then has to re-adjust the font size again for each page they visit. It would be nice if once the visitor adjusts the page size on one page and then goes to another page, that other pager would automatically appear in the adjusted font size. Thank you, Chris
Chris
You will need to save the values in a cookie. You are better off asking for this in a new question, as it is now a new issue (and you'll get more attention). If my answer was the solution for you, you can always mark it as the "accepted answer" by click on the tick next to it.
Sinan Taifour
Hi Mr. STAii. Thank you for your help. I will post a new question. If I can find the box next to "accepted answer", I will click on it. Thank you, Chris
Chris
Hi Mr. STAii. I do not seem to be able to find "accepted answer" with a box next to it that I can tick. I assume me ticking this box will be of benefit to you. If you can tell me where this box is in more detail, I would be happy to tick it. Chris
Chris
A: 

Taking what you have made, what you could do is that instead of using doIncreaseSize as a boolean, you could instead call that variable action and do

...
if (action == 'sub') {
    unit = -unit;
}
newVal = currentSize - unit;
if (action == 'def') {
    newVal = some_default_value;
}
...

But the real question here is why are you doing this in the first place? It would probably be a lot easier just setting the value instead of increasing and decreasing, unless you plan to make a button that the user can click to increase and decrease the text size of your webpage. But like posted in the comments, that can be done in the browser directly.

googletorp
Hi Googletorp, thank you for your help. Yes, I plan on making a button allowing the user to increase / decrease the font size. Chris
Chris