faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 7 of 8 people (88%) answered Yes

Entry

If one checkbox is selected dynamically the other checkboxes are selected

Apr 8th, 2008 19:13
ha mo, Dave Clark, tamil arasi,


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tamil,
     The following function will work for that use whether the 
checkboxes all have the same NAME attribute value or each checkbox has 
a unique NAME attribute value:
function CheckAll(ctl, grp, lbl)
{
    var x, len = grp.length;
    for (x=0; x<len; ++x)
    {
        grp[x].checked = ctl.checked;
    }
    if (lbl)
    {
        var obj = document.getElementById(lbl);
        if (ctl.checked)
            obj.innerHTML = ' Uncheck All';
        else
            obj.innerHTML = ' Check All';
    }
    return true;
}
     The following is a sample form showing how to code for checkboxes 
all with the same name:
<form action="">
  <p><input type="checkbox" value="ALL" id="cb1"
   onclick="return CheckAll(this, c1, 'lb1')"><label
   id="lb1" for="cb1"> Check All</label></p>
  <p>
  1. <input type="checkbox" name="c1" value="1"><br>
  2. <input type="checkbox" name="c1" value="2"><br>
  3. <input type="checkbox" name="c1" value="3"></p>
  <p><input type="submit" value="Submit"></p>
</form>
     The following is a sample form showing how to code for checkboxes 
with unique names:
<form action="">
  <p><input type="checkbox" value="ALL" id="cb1"
   onclick="return CheckAll(this, [c1, c2, c3], 'lb1')"><label
   id="lb1" for="cb1"> Check All</label></p>
  <p>
  1. <input type="checkbox" name="c1" value="1"><br>
  2. <input type="checkbox" name="c2" value="2"><br>
  3. <input type="checkbox" name="c3" value="3"></p>
  <p><input type="submit" value="Submit"></p>
</form>
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com