[Phpwm] [OT] JavaScript Scope Problem

pete graham petegraham1 at gmail.com
Fri Aug 10 16:44:12 BST 2007


I'm writing some Javascript that highlights rows in a html table when
checkboxes are ticked (like gmail). However since I initialise the
function onload the bottom row in the table always gets highlighted no
matter which box is ticked.

Here is an extract of the code:
WindowOnload(function()
{
    var form = document.getElementById('clips_form');
    var re = new RegExp('^mod_[0-9]+$');

    // search through forms children for checkboxes
    for (i=0;i<form.elements.length;i++)
    {
        if (form.elements[i].type=='checkbox')
        {
            // Find checkbox for each member of the group:
            if (re.test(form.elements[i].name))
            {
                box = form.elements[i];
                box.onclick = function(){
                    return highlight_row(box);
                };
            }
        }
    }
});

(I can provide more code or explanations of the code if needed)
I remember this irritating me before, ggrrrhhh. There is a
work-around/hack that I can't remember which makes JavaScript variable
scope behave itself, it involves declaring functions all over the
place.

Thaks, Pete



More information about the Phpwm mailing list