Show/Hide a bunch of elements, without using Prototype.js

May 20th, 2008

Today I worked on a simple page. I need to show a few table rows when a button is clicked. Normally I would use Prototype.js but it seemed to be an overkill for the simple I was building.

So I decided to just use javascript without any libraries. Here is what I wrote (referenced a good site found on the Internet):

function calc(cls_name) {
  if (document.getElementsByTagName) {
         var elements = document.getElementsByTagName("*");
         var check = new RegExp('/b^|'+cls_name+'|$/b');
         for (var i=0; i < elements.length; i++) {
              if (elements[i].className) {
                   if(check.test(elements[i].className)) {
                      // match, show element
                      elements[i].style.display='block';
                   }
              }
         }
    }
}

Please add your comment