Radio Button Status Change....

Joined: 11/28/2008

Ok I know this just HAS to be easy and I'm missing it due to my lack of javascript knowledge.

Thus I'm asking the oh great javascript guru's of CWM.

** How's that for some brown nosing. /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> **

To the problem:

I've got some radio buttons in a grid.

What I want to happen is when one is selected a group of others is selected.

The javascript I have to do this is:

CODE
function selectAllMon() {
    document.forms[1].mornM.checked = true;
    document.forms[1].dayM.checked = true;
    document.forms[1].nightM.checked = true;
}

I have this function triggered here:

CODE
<input type="radio" id="allM" name="allM" value="Yes" onClick="selectAllMon()" />

It appears to work behind the scenes in both IE6 and FF.

Unfortunately, the visual status of the check boxes listed in the javascript are not showing to be checked.

I've been banging my head against this all morning but can not figure it out. /blink.gif" style="vertical-align:middle" emoid=":blink:" border="0" alt="blink.gif" />

What am I missing oh great gurus? /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Joined: 11/28/2008
I tested your code with a

I tested your code with a minor change from "forms[1]" to "forms[0]" and it appears to work.

CODE
<html>
<body>
<script language=javascript>
function selectAllMon() {
    document.forms[0].mornM.checked = true;
    document.forms[0].dayM.checked = true;
    document.forms[0].nightM.checked = true;
}
</script>
<form id="test">
<input type="radio" id="allM" name="allM" value="Yes" onClick="selectAllMon()" />
<input type="checkbox" id="mornM" name="mornM" value="Yes" />
<input type="checkbox" id="dayM" name="dayM" value="Yes" />
<input type="checkbox" id="nightM" name="nightM" value="Yes" />
</form>
</body>
</html>
Joined: 11/28/2008
Man do I feel like a dummy.I

Man do I feel like a dummy.

I name of form changed.

Once I set the javascript to point to document.formname.... everything started working.

BTW, if you only have one form on the page then yes the [0] would be correct.

I was referencing the second form on the page, thus the form[1]. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Joined: 11/28/2008
Ah yes, you didn't specify so

Ah yes, you didn't specify so I assumed 1 form on the page, hence 0. Glad you found your problem!

Joined: 11/28/2008
it's best to not use things

it's best to not use things like document.forms or document.images etc... especially not document.all.

Keep your DOM references to an {HTMLElement} using core Dom methods like getElementById or getElementsByTagName etc...

Joined: 11/28/2008
One other thing, I wrote a

One other thing, I wrote a nifty widget that allows you to toggle fieldsets with specified enablers. As long as you use the right class names etc. you'd be good to go (I wrote this about a year ago, so there's definitely a more efficient way to do this now):

CODE
var groupTog = {
    findSrc : function(e) {
        var obj = getEventSrc(e);
        var tgt = obj.getAttribute('rel');
        groupTog.applyActions(tgt);
    },
    applyActions : function(targets) {
        var targetGroups = new Array();
        targetGroups = targets.split(' ');
        var groups = getElementsByClass(document,'togGroup','*');
        var groupsLen = groups.length;
        for ( i=0;i<groupsLen;i++ ) {
            var gId = groups[i].id;
            if ( targetGroups.inArray(gId) ) {
                // enable groups
                groups[i].className = (groups[i].className).toString().replace('dis','');
            }
            else {
                // disable groups
                groups[i].className = (groups[i].className).toString().replace('dis','');
                groups[i].className += ' dis';
            }
        }
    },    
    initToggleState : function() {
        var enablers = getElementsByClass(document,'togEnable','*');
        for ( i=0;i<enablers.length;i++ ) {
            if ( enablers[i].checked == true ) {
                groupTog.applyActions(enablers[i].getAttribute('rel'));
            }
        }
    },
    initTogglers : function() {
        var enablers = getElementsByClass(document,'togEnable','*');
        for ( i=0;i<enablers.length;i++ ) {
            addEvent(enablers[i],'click',groupTog.findSrc,false);
        }
    }
};
addLoadEvent(groupTog.initTogglers);
addLoadEvent(groupTog.initToggleState);

Granted, you'll need these helper functions:

CODE
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
    }
    else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    return r;
    }
    else {
    elm['on' + evType] = fn;
    }
}
function getEventSrc(e) {
    if (!e) e = window.event;

    if (e.originalTarget)
    return e.originalTarget;
    else if (e.srcElement)
    return e.srcElement;
}
function addLoadEvent(func) {
var oldonload = window.onload;
    if (typeof window.onload != 'function') {
    window.onload = func;
    } else {
    window.onload =
        function() {
        oldonload();
        func();
        }
    }
}
Array.prototype.inArray = function (value) {
    var i;
        for (i=0; i < this.length; i++) {
            if (this[i] == value)
            return true;
        }
    return false;
    };

Of course without a working demo for you to see this, I can see how it's easy to get lost. I'll get a working demo of this on my site soon enough to help give you a visual.