Monday, July 18, 2005

Love

I have a love/hate relationship with javascript.
  • Hate: it's awful to debug, and for some reason I never get it right from the first try.
  • Love: the power of it's 'function()'-statement.

    Let me explain; on one fine day I needed an iteration over a list of checkboxes (with id's/names like 'chkItem1', 'chkItem2', ..., 'chkItemN') to
    A. select them all.
    B. invert the selection.

    My code lookes like this:

    // (A) Select them all.
    function selectAll()
    {
    var evaluator = function(chk) {

    return true;
    };

    makeSelection('chkItem', evaluator);
    }

    // (B) Invert selection.
    function invertSelection()
    {
    var evaluator = function(chk) {

    return !chk.checked;
    };

    makeSelection('chkItem', evaluator);
    }

    var makeSelection = function(checkboxName, evaluator)
    {
    for (i = 1; true; i++)
    {
    if (!(chk = document.getElementById(checkboxName + i)))
    {
    break;
    }

    chk.checked = evaluator(chk);
    }
    }

     
    The concerning webpage has some buttons to invoke 'javascript:selectAll();' and 'javascript:invertSelection();'.

7 comments:

Webmaster said...

Hello, I was checking out the blogs on blogger.com for relationship and dating blogs singles online dating match I ended up finding your blog, avery interesting blog, by accident, I am certainly glad I did. You have a Tremendous blog, would you mind if I link to your blog in my "favorites" page?

Legal Forms said...

Hi, I was blog surfing for the latest information on relationship sitesdating and ended up on your blog. Obviously your blog came up in my search, but I'm certainly glad I did! Would you mind if I link to your blog in my "favorites" page?

Webmaster said...

Hi, I was blog surfing for the latest information on relationship sitesrelationship problems and ended up on your blog. Obviously your blog came up in my search, but I'm certainly glad I did! Would you mind if I link to your blog in my "favorites" page?

Webmaster said...

Hi, I was blog surfing for the latest information on relationship sitesRelationship Articles and ended up on your blog. Obviously your blog came up in my search, but I'm certainly glad I did! Would you mind if I link to your blog in my "favorites" page?

Legal Forms said...

Hello very interesting blog you got here! kudos, I was searching for dating sitesonline dating I am going to add you to my favorites page liked your blog a lot. Hope you do not mind

Legal Forms said...

Hello very interesting blog you got here! kudos, I was searching for dating sites I am going to add you to my favorites page liked your blog a lot. Hope you do not mind

Legal Forms said...

Hello, I was checking out the blogs on blogger.com for relationship and dating blogs find love I ended up finding your blog, avery interesting blog, by accident, I am certainly glad I did. You have a Tremendous blog, would you mind if I link to your blog in my "favorites" page?