Re: javascript checkbox help

by "apathetic" <tmbkr(at)apatheticgenius.com>

 Date:  Tue, 22 Jan 2002 23:33:55 -0000
 To:  "Davies,
Elizabeth H." <EHDavies(at)West.com>
 Cc:  <hwg-techniques(at)hwg.org>
 References:  wtc
  todo: View Thread, Original
> Does anyone have a script that will select only part of a group of
checkboxes on a form?

Elizabeth,

If you know the number of form elements on the page, then you can loop
through them by numerical index:

<input name="chkAll" onClick="checkAll( 20, 40, this.checked )" />

function checkAll( start, end, bool ) {

    var form = document.forms[ 'frmMain' ];

    for( var i = start; i <= end; i++ ) {
        form[ i ].checked = bool;
    }
}

Or, you could give the checkboxes names which correspond to their
group, ie:

<input name="chk1BlahBlah" />

<input name="chkAll" onClick="checkAll( 'chk1', this.checked )" />

function checkAll( grpName, bool ) {

    var form = document.forms[ 'frmMain' ];

    for( elm in form ) {
        if( form[ elm ].name.substring( 0, ( grpName.length - 1 ) ) ==
grpName ) {
            form[ elm ].checked = bool;
        }
    }
}


This is just quick; I probably have some errors in the code, but it's
the general idea.

Tim

http://www.yaffle.org/

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA