Fork me on GitHub

article

Javascript table row highlighting. Then and Now.

February 12, 2009 | Web Design & Development

Just so that you’re sufficiently warned: This post offers no practical information. Its a "remember when? didn’t that suck?" post.

I think it was 2004. Prototype.js was new and was rocking my world. But it was still just a few convenience features put on top of JavaScript. I needed to stripe table rows dynamically and ended up with 2 functions and ~25 lines of code. My, how things have changed.

So, with a litle help from Prototype.js I had this:

  1.  // js function to stripe the rows of a table
  2.  function stripeTable() {
  3.   var stripeMe = document.getElementsByClassName('stripeme');
  4.   if (!stripeMe) { return; }
  5.   for(var s = 0; s < stripeMe.length; s++) {
  6.   var teeBodies = stripeMe[s].getElementsByTagName('tbody');
  7.   // loop through tbody elements
  8.   for (var c = 0; c < teeBodies.length; c++) {
  9.   //get TRs in tbody
  10.   var teeRs = teeBodies[c].getElementsByTagName('tr');
  11.   // loop through rows and assign even numbered, non styled rows a new class
  12.   for (var i = 0; i < teeRs.length; i++) {
  13.   if(i % 2 == true) {
  14.   if(isStyled(teeRs<i>) == false) { teeRs<i>.className = 'stripeme_two'; }
  15.   else if(isStyled(teeRs<i>) !== false) { teeRs<i>.className = teeRs<i>.className + ' stripeme_two'; }
  16.   }
  17.   }
  18.   }
  19.   }
  20.  }
  21.  // gleened from ALA, see if an object has styling
  22.  function isStyled(obj) {
  23.   var result = false;
  24.   if (obj.getAttributeNode("class") != null) { result = obj.getAttributeNode("class").value; }
  25.   return result;
  26.  }
  27.  Event.observe(window,'load',stripeTable,false);

Damn n00b! Look at all that code. Now lets step to 2 months ago. Striping tables with jQuery turns in to:

  1.  jQuery(".stripe_me tbody tr:odd").addClass("stripe");

With a few more chained items I could add in JS row rollover highlighting (because IE hasn’t implemented the CSS to do it yet, that’s how they do!). I’m so digging other people doing the nitty gritty.

I’d like to welcome myself to 2008. 😉