.Net ramblings
# Thursday, 02 February 2006
CheckBoxList: Select All / None client-side script
<script language="javascript" type="text/javascript">
function Select(Select)
{
for (var n=0; n < document.forms[0].length; n++)
if (document.forms[0].elements[n].type=='checkbox')
document.forms[0].elements[n].checked=Select;
return false;
}
</script>


Select <a href="#" onclick="javascript:Select(true)">All</a> | <a href="#" onclick="javascript:Select(false)">None</a>


Thursday, 02 February 2006 13:25:33 (GMT Standard Time, UTC+00:00)  #    Comments [7]  Asp.Net

Friday, 14 July 2006 02:52:35 (GMT Daylight Time, UTC+01:00)
How could this code be modified to check/uncheck all the checkboxes in a single CheckBoxList when multiple CheckBoxLists exists on the same page? CheckBoxList doesn't give all the checkboxes the Name value so attempting to group checkboxes by Name isn't possible.
Michael Hanes
Friday, 14 July 2006 07:56:42 (GMT Daylight Time, UTC+01:00)
Well it's probably not perfect but this might do the trick:

function selectAll (listId)
{
for (index = 0; index < checkBoxes.length; index++)
{
var currentCheckBox = checkBoxes [index];

if (currentCheckBox != null)
{
var currentCheckBoxId = currentCheckBox.id;

var lastUnderscoreIndex = currentCheckBoxId.lastIndexOf ('_');
var currentCheckBoxIdBaseId =
currentCheckBoxId.substring (0, lastUnderscoreIndex);

if (currentCheckBoxIdBaseId == listId)
{
currentCheckBox.checked = true;
}
}
}
}
Michael Hanes
Wednesday, 16 April 2008 18:40:17 (GMT Daylight Time, UTC+01:00)
function selectSwitch (listId, isChecked)
{
for (index = 0; index < document.forms[0].length; index++)
{
if (document.forms[0].elements[index].type=='checkbox')
{
var currentCheckBox = document.forms[0].elements[index];

if (currentCheckBox != null)
{
var currentCheckBoxId = currentCheckBox.id;

var lastUnderscoreIndex = currentCheckBoxId.lastIndexOf ('_');
var currentCheckBoxIdBaseId =
currentCheckBoxId.substring (0, lastUnderscoreIndex);

if (currentCheckBoxIdBaseId == listId)
{
currentCheckBox.checked = isChecked;
}
}
}
}
}
Two Years Too Late
Monday, 06 October 2008 12:36:35 (GMT Daylight Time, UTC+01:00)
How could this code be modified to check/uncheck all the checkboxes in a single CheckBoxList when multiple CheckBoxLists exists on the same page?
Hemant
Monday, 06 October 2008 12:38:21 (GMT Daylight Time, UTC+01:00)
hi hemant. have a read of the other comments, it's been asked and answered before! good luck. tim.
tim mackey
Wednesday, 30 September 2009 15:33:52 (GMT Daylight Time, UTC+01:00)
try this solution http://praveenbattula.blogspot.com/2009/09/aspnet-checkboxlist-get-values-in.html
Wednesday, 30 September 2009 15:34:47 (GMT Daylight Time, UTC+01:00)
Try this for simple logic to check and uncheck all or in specific div.
http://praveenbattula.blogspot.com/2009/09/checkuncheck-all-checkboxes-in-jquery.html
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview