Blah Blah Woof Woof

Archived content 2005-2007

Ready to Rumble

Posted by Tim Riley Fri, 07 Sep 2007 08:36:00 GMT

This weekend, Hugh and I will spend 48 hours building a new web app as part of the Rails Rumble. We're going to build something that I have personally wanted to use for a long time. It should be a lot of fun!

We may be looking for some beta testers, so feel free to register your interest here. Anyway, watch this space, I will keep you all posted throughout the event!

Posted in Life, Geek | 6 comments

CSS selectors in Prototype are a godsend

Posted by Tim Riley Fri, 07 Sep 2007 07:51:00 GMT

This week I have been working in some parts of an app that were written a long time ago and have not been touched since.

I've had to resist the temptation to clean up all the code, but a couple of things I could not just let be.

Behold the ugly, ugly, JS code used to select checkboxes within a particular div:

/* Iterates through all users within the TBODY and checks their checkboxes */ 
function markCheckboxesForDiv(tbody, value) { 
  for (i = 0; i < tbody.childNodes.length; i++) { 
    if (tbody.childNodes[i].tagName == 'DIV') { 
      for (j = 0; j < tbody.childNodes[i].childNodes.length; j++) { 

        if (tbody.childNodes[i].childNodes[j].tagName == 'DIV') { 
          for (k = 0; k < tbody.childNodes[i].childNodes[j].childNodes.length; k++) { 

            if (tbody.childNodes[i].childNodes[j].childNodes[k].tagName == 'SPAN') { 
              for (l = 0; l < tbody.childNodes[i].childNodes[j].childNodes[k].childNodes.length; l++) { 

                if (tbody.childNodes[i].childNodes[j].childNodes[k].childNodes[l].tagName == 'INPUT') { 
                  tbody.childNodes[i].childNodes[j].childNodes[k].childNodes[l].checked = value; 

Apart from the obvious problems with the depply nested if statements, this code is strongly dependent on the structure of the page markup. I actually broke it while making some unrelated changes.

Here is the new version:

function markCheckboxesForDiv(divname, value) {
  $$('#' + divname + ' input[type=checkbox]').each(function(f) {
    f.checked = value;

Much simpler, more robust and more loosely coupled thanks to the wonders of the CSS selector utility function from Prototype, dollar dollar.

Posted in Geek | no comments