Forums

"check all" checkbox

wdemilly 30 Jan, 2010
I'm building a form which has a table of 20 rows of information.

Each row will have a checkbox by it (as in "please send me more info on this").

I'd like to add a "check all" checkbox at the top of the table. Any suggestions on how to do this?

Thanks!!
GreyHead 30 Jan, 2010
Hi wdemilly,

Borrow it, there's a checkAll function in the main Joomla admin javascript file:
/**
253* Toggles the check state of a group of boxes
254*
255* Checkboxes must have an id attribute in the form cb0, cb1...
256* @param The number of box to 'check'
257* @param An alternative field name
258*/
259function checkAll( n, fldName ) {
260 if (!fldName) {
261 fldName = 'cb';
262 }
263 var f = document.adminForm;
264 var c = f.toggle.checked;
265 var n2 = 0;
266 for (i=0; i < n; i++) {
267 cb = eval( 'f.' + fldName + '' + i );
268 if (cb) {
269 cb.checked = c;
270 n2++;
271 }
272 }
273 if (c) {
274 document.adminForm.boxchecked.value = n2;
275 } else {
276 document.adminForm.boxchecked.value = 0;
277 }
278} 
You'll need to remove the line numbers and tweak the code.

Bob
wdemilly 31 Jan, 2010
Bob, thanks! I also found a js snippet (below). If I use the joomla code I assume it will go into the php section in form edit?
W.

<SCRIPT LANGUAGE="JavaScript">
<!-- 	
// by Nannette Thacker
// http://www.shiningstar.net
// This script checks and unchecks boxes on a form
// Checks and unchecks unlimited number in the group...
// Pass the Checkbox group name...
// call buttons as so:
// <input type=button name="CheckAll"   value="Check All"
	//onClick="checkAll(document.myform.list)">
// <input type=button name="UnCheckAll" value="Uncheck All"
	//onClick="uncheckAll(document.myform.list)">
// -->

<!-- Begin
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}
//  End -->
</script>


<form name="myform" action="checkboxes.asp" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">Java<br>
<input type="checkbox" name="list" value="2">Javascript<br>
<input type="checkbox" name="list" value="3">Active Server Pages<br>
<input type="checkbox" name="list" value="4">HTML<br>
<input type="checkbox" name="list" value="5">SQL<br>

<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list)">
<input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.myform.list)">
<br>
</form>
GreyHead 31 Jan, 2010
Hi wdewmilly,

The 'script' part - without the script tags - goes into the Form JavaScript box. (There is no PHP section as such.) The HTML goes into the Form HTML box - without the Form tags.

But, like the Joomla code that I posted you will need to tweak the code to get it to work. in particular ChronoForms changed the form name so you'll need to make sure that matches.

Bob
This topic is locked and no more replies can be posted.