tags:

views:

19

answers:

2

Hello Gurus.

I have this simple font resize build with JavaScript. It resizes app p tags on page but I need to add other tags, like li. How can I do that. Any help much appreciated.

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"
   }   
}

Many Thanks for your help in advance.

Dom

A: 

Use querySelectorAll to use CSS selectors, e.g.:

var nodes = document.querySelectorAll('p, li');
Delan Azabani
Note: Not supported by IE6/7 and FF < 3.5. http://www.quirksmode.org/dom/w3c_core.html
Pekka
Thanks for that. I couldn't quite think of another way to do multiple-tag selection without manually joining the NodeList objects after two tag selections. Of course, there's jQuery, but I don't like the use of "general" JavaScript libraries.
Delan Azabani
A: 

If your pattern is simple (eg "p li") and don't want to use javascript libraries, you can use document.createTreeWalker

dafi