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

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’;
                   }
              }
         }
    }
}

Social Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • TwitThis
RSS 2.0 | Trackback | Comment

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>