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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox toggling and checkall validation - clamshell navigation 1

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
I need help with my ValidateCheckAll function. When I click on an individual checkbox, I want to check all other checkboxes in that group to determine if I need to check the top-level checkbox for the designated group.

Also, after reviewing my code for this functionality, if there is an easier (less static) way to go about this, please advise.

Below is my code for this functionality. Also, here is an image of how the page renders.

usermgmt.gif


Thanks a ton

<html>
<head>
<title>User Management</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript1.2&quot; type=&quot;text/javascript&quot;>
<!--
var isDHTML = 0;
var isID = 0;
var isAll = 0;
var isLayers = 0;

//Uses feature sensing to determine browser capabilities
if (document.getElementById) {isID = 1; isDHTML = 1;}
else {
if (document.all) {isAll = 1; isDHTML = 1;}
else {
browserVersion = parseInt(navigator.appVersion);
if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
}}

//Finds the location of thd DOM and the ability to access the style properties
function findDOM(objectID,withStyle) {
if (withStyle == 1) {
if (isID) { return (document.getElementById(objectID).style) ; }
else {
if (isAll) { return (document.all[objectID].style); }
else {
if (isLayers) { return (document.layers[objectID]); }
};}
}
else {
if (isID) { return (document.getElementById(objectID)) ; }
else {
if (isAll) { return (document.all[objectID]); }
else {
if (isLayers) { return (document.layers[objectID]); }
};}
}
}


//Hides/Shows the side navigation window
function toggleUserMenu(objectID) {
if (isAll || isID) {
domStyle = findDOM(objectID,1);
if (domStyle.display =='block') domStyle.display='none';
else domStyle.display='block';
}
return;
}

//Selects/De-selects all checkboxes for appropriate group if top-level checkbox is clicked
function ToggleAll(e,which) {
if (e.checked) {
CheckAll(which);
} else {
ClearAll(which);
}
}

//Selects/De-selects the clicked checkbox, but also validates all other checkboxes to
//determine if top-level checkbox should be selected.
function ToggleThis(e,which) {
switch (which) {
case &quot;ckViewAllInd&quot;: case &quot;ckViewAllRoles&quot;:
if (e.checked) {
ValidateCheckAll(&quot;ckViewAllInd&quot;);
ValidateCheckAll(&quot;ckViewAllRoles&quot;);
} else {
Clear(e,which);
}
break;
case &quot;ckNotifyAllInd&quot;: case &quot;ckNotifyAllRoles&quot;:
if (e.checked) {
ValidateCheckAll(&quot;ckNotifyAllInd&quot;);
ValidateCheckAll(&quot;ckNotifyAllRoles&quot;);
} else {
Clear(e,which);
}
break;
}
}

//Selects the current checkbox
function Check(e) {
e.checked = true;
}

//De-selects the current checkbox
function Clear(e,which) {
e.checked = false;
if (which != '') {
eval(&quot;frmUsers.&quot; + which + &quot;.checked = false;&quot;);
}
}

//Displays debuggin information
function alertCheck(e,which,pass) {
var n = &quot;\n&quot;;
var msg = &quot;PASS # &quot; + pass;
msg += n + &quot;---------------------------------------&quot;;
msg += n + &quot;which=&quot; + which;
msg += n + &quot;e.value=&quot; + e.value;
msg += n + &quot;e.checked=&quot; + e.checked;
return alert(msg);
}

//global variables to show me if all checkboxes for a given group are selected ( problematic possibly )
var allViewIndChecked=true;
var allViewRoleChecked=true;
var allNotifyIndChecked=true;
var allNotifyRoleChecked=true;

//Checks whether to select the top-level checkbox ( problematic area )
function ValidateCheckAll(which) {

var frm = document.frmUsers;
var len = frm.elements.length;
var ipass=0;
for (var i = 0; i < len; i++) {
ipass += 1;
var e = frm.elements;
if (!e.checked) {
switch (e.name) {
case &quot;ckViewInd&quot;:
allViewIndChecked=false;
break;
case &quot;ckViewRole&quot;:
allViewRoleChecked=false;
break;
case &quot;ckNotifyInd&quot;:
allNotifyIndChecked=false;
break;
case &quot;ckNotifyRole&quot;:
allNotifyRoleChecked=false;
break;
}
}
}

if (allViewIndChecked) {
CheckAll(&quot;ckViewAllInd&quot;);
} else if (allViewRoleChecked) {
CheckAll(&quot;ckViewAllRoles&quot;);
} else if (allNotifyIndChecked) {
CheckAll(&quot;ckNotifyAllInd&quot;);
} else if (allNotifyRoleChecked) {
CheckAll(&quot;ckNotifyAllRoles&quot;);
} else {
eval(&quot;frm.&quot; + which + &quot;.checked = true;&quot;);
return true;
}
}

//Selects all the checkboxes in a given group
function CheckAll(which) {
var frm = document.frmUsers;
var len = frm.elements.length;
for (var i = 0; i < len; i++) {
var e = frm.elements;
switch (which) {
case &quot;ckViewAllInd&quot;:
if (e.name == &quot;ckViewInd&quot;) {
Check(e);
}
break;
case &quot;ckViewAllRoles&quot;:
if (e.name == &quot;ckViewRole&quot;) {
Check(e);
}
break;
case &quot;ckNotifyAllInd&quot;:
if (e.name == &quot;ckNotifyInd&quot;) {
Check(e);
}
break;
case &quot;ckNotifyAllRoles&quot;:
if (e.name == &quot;ckNotifyRole&quot;) {
Check(e);
}
break;
}
}

eval(&quot;frm.&quot; + which + &quot;.checked = true;&quot;);

if (which == &quot;ckNotifyAllInd&quot;) {
allNotifyIndChecked=true;
} else if (which == &quot;ckNotifyAllRoles&quot;) {
allNotifyRoleChecked=true;
} else if (which == &quot;ckViewAllInd&quot;) {
allViewIndChecked=true;
} else if (which == &quot;ckViewAllRoles&quot;) {
allViewRoleChecked=true;
}
}

//De-selects all the checkboxes in a given group
function ClearAll(which) {
var frm = document.frmUsers;
var len = frm.elements.length;
for (var i = 0; i < len; i++) {
var e = frm.elements;
switch (which) {
case &quot;ckViewAllInd&quot;:
if (e.name == &quot;ckViewInd&quot;) {
Clear(e,'');
viewIndFlag=false;
}
break;
case &quot;ckViewAllRoles&quot;:
if (e.name == &quot;ckViewRole&quot;) {
Clear(e,'');
viewRoleFlag=false;
}
break;
case &quot;ckNotifyAllInd&quot;:
if (e.name == &quot;ckNotifyInd&quot;) {
Clear(e,'');
NotifyIndFlag=false;
}
break;
case &quot;ckNotifyAllRoles&quot;:
if (e.name == &quot;ckNotifyRole&quot;) {
Clear(e,'');
NotifyRoleFlag=false;
}
break;
}
}

eval(&quot;frm.&quot; + which + &quot;.checked = false;&quot;);

if (which == &quot;ckNotifyAllInd&quot;) {
allNotifyIndChecked=false;
} else if (which == &quot;ckNotifyAllRoles&quot;) {
allNotifyRoleChecked=false;
} else if (which == &quot;ckViewAllInd&quot;) {
allViewIndChecked=false;
} else if (which == &quot;ckViewAllRoles&quot;) {
allViewRoleChecked=false;
}
}

// -->

</script>
</head>

<body>

DUE TO SPACE ISSUES, CODE TRUNCATED TO JUST INCLUDE JAVASCRIPT CALLS

<form name=&quot;frmUsers&quot;>

<th width=&quot;200&quot; align=&quot;center&quot;>Name<br/> </th>
<th width=&quot;50&quot; class=&quot;centerCheck&quot;>View<!--<br/><input id=&quot;ckViewAll&quot; name=&quot;ckViewAll&quot; type=&quot;checkbox&quot; value=&quot;viewAll&quot; onClick=&quot;ToggleAll(this,'ckViewAll');&quot;>--></th>
<th width=&quot;50&quot; class=&quot;centerCheck&quot;>Notify<!--<br/><input id=&quot;ckNotifyAll&quot; name=&quot;ckNotifyAll&quot; type=&quot;checkbox&quot; value=&quot;notifyAll&quot; onClick=&quot;ToggleAll(this,'ckNotifyAll');&quot;>--></th>

<td width=&quot;50&quot; class=&quot;centerCheck&quot;><input id=&quot;ckViewAllInd&quot; name=&quot;ckViewAllInd&quot; type=&quot;checkbox&quot; value=&quot;viewIndAll&quot; onClick=&quot;ToggleAll(this,'ckViewAllInd');&quot;></td>
<td width=&quot;50&quot; class=&quot;centerCheck&quot;><input id=&quot;ckNotifyAllInd&quot; name=&quot;ckNotifyAllInd&quot; type=&quot;checkbox&quot; value=&quot;notifyIndAll&quot; onClick=&quot;ToggleAll(this,'ckNotifyAllInd')&quot;></td>

<td width=&quot;188&quot; class=&quot;nameIndent&quot;><%response.write(rs(&quot;FirstName&quot;) & &quot; &quot; & rs(&quot;LastName&quot;))%></td>
<td width=&quot;56&quot; align=&quot;center&quot;><input id=&quot;ckViewInd&quot; name=&quot;ckViewInd&quot; type=&quot;checkbox&quot; value=&quot;<% response.write(rs(&quot;UserId&quot;))%>&quot; onClick=&quot;ToggleThis(this,'ckViewAllInd');&quot;></td>
<td width=&quot;56&quot; align=&quot;center&quot;><input id=&quot;ckNotifyInd&quot; name=&quot;ckNotifyInd&quot; type=&quot;checkbox&quot; value=&quot;<% response.write(rs(&quot;UserId&quot;))%>&quot; onClick=&quot;ToggleThis(this,'ckNotifyAllInd');&quot;></td>

<td width=&quot;50&quot; class=&quot;centerCheck&quot;><input id=&quot;ckViewAllRoles&quot; name=&quot;ckViewAllRoles&quot; type=&quot;checkbox&quot; value=&quot;viewRoleAll&quot; onClick=&quot;ToggleAll(this,'ckViewAllRoles');&quot;></td>
<td width=&quot;50&quot; class=&quot;centerCheck&quot;><input id=&quot;ckNotifyAllRoles&quot; name=&quot;ckNotifyAllRoles&quot; type=&quot;checkbox&quot; value=&quot;notifyRoleAll&quot; onClick=&quot;ToggleAll(this,'ckNotifyAllRoles');&quot;></td>

<td width=&quot;50&quot; class=&quot;centerCheck&quot;><input id=&quot;ckViewRole&quot; name=&quot;ckViewRole&quot; type=&quot;checkbox&quot; value=&quot;<% response.write(rs(&quot;RoleId&quot;))%>&quot; onClick=&quot;ToggleThis(this,'ckViewAllRoles');&quot;></td>
<td width=&quot;50&quot; class=&quot;centerCheck&quot;><input id=&quot;ckNotifyRole&quot; name=&quot;ckNotifyRole&quot; type=&quot;checkbox&quot; value=&quot;<% response.write(rs(&quot;RoleId&quot;))%>&quot; onClick=&quot;ToggleThis(this,'ckNotifyAllRoles');&quot;></td>

</form>
</body>
</html>
regards,
Brian

AG00280_.gif
 
here's one way:
[tt]
function checkGroup(oTopLevel,oGroup) {
var bInitialState;
var bDifferent = false;
for (x = 0; x < oGroup.length; x++) {
if (x == 0)
bInitialState = oGroup[x].checked;
if (oGroup[x].checked != bInitialState)
bDifferent = true;
}
if (bDifferent) oTopLevel.checked = false;
else oTopLevel.checked = bInitialState;
}
[/tt]


call it like so:
[tt]
checkGroup(document.forms[0].ckViewAllInd,document.forms[0].ckViewInd);
[/tt]
=========================================================
if (!succeed) try();
-jeff
 
This worked great for what I was looking for. I like the fact that the code is much more dynamic than what i had. Thanks regards,
Brian

AG00280_.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top