Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unchecking dynamically checked checkboxes. 1

Status
Not open for further replies.

travisbrown

Technical User
Joined
Dec 31, 2001
Messages
1,016
Once more with feeling.

I'm trying to get all ckeckboxes within a div (regardless of child nodes) to check/uncheck based on the state of a master checkbox. Dan's post at is where I'm heading, but haveing a few implementation problems. I'm not sure how to get them unchecked. Can anyone help?

Code:
function CheckAll(theElement)
{
var cbs = document.getElementById(theElement).getElementsByTagName('input');
for (var loop=0; loop<cbs.length; loop++)
   if (cbs[loop].type == 'checkbox') cbs[loop].checked = true;
}

Code:
<input name="checkall" id="checkall" type="checkbox" value="" onclick="CheckAll('recipientsList');" />
 
I was thinking something like this but I usually cause more problems for myself than good. Am I on the right track?

Code:
<input name="checkall" id="checkall" type="checkbox" value="" onclick="CheckAll('recipientsList',this);" />

function CheckAll(theElement,control)
{
var cbs = document.getElementById(theElement).getElementsByTagName('input');

if (GetElementById(control).checked = false) {
for (var loop=0; loop<cbs.length; loop++)
   if (cbs[loop].type == 'checkbox') cbs[loop].checked = true; }
else
{
for (var loop=0; loop<cbs.length; loop++)
   if (cbs[loop].type == 'checkbox') cbs[loop].checked = false; }
}
 
slight mod:

Code:
function CheckAll(theElement,control) {
    var cbs = document.getElementById(theElement).getElementsByTagName('input');

    for (var loop=0; loop<cbs.length; loop++)
        if (cbs[loop].type == 'checkbox') cbs[loop].checked = control.checked;
}



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
It looks good and much slicker than my else attempt, but not checking the checkboxes now. It will uncheck any boxes onclick.
 
You are right. Impossible. When monkeying around before you replied I'd changed this:

CheckAll('recipientsList',this);
to this
CheckAll('recipientsList','checkall');

Changed it back and all is well.

Star for you.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top