toggle checkboxes javascript using prototype

Toggle All Check Boxes using prototype javascript library.

function toggleCheckBoxes(formName)   {
// toggle Check Boxes using Prototype Library
	var form=$(formName);
	var i=form.getElements('checkbox');
	i.each(function(item)
		{
		if (item.checked){
			item.checked=false;
		}
		else {
			item.checked=true;
		}
		}
	);
} 

to call use no quotes around the form name:
 onClick="toggleCheckBoxes(formName);"

Another example using some prototype shorthand to enable and disable a single checkbox:

for this input checkbox element:

< input id="EXCESS_AMT0" name="isNotUsed" type="checkbox" >

which has the id=”EXCESS_AMT0″

we can manipulate this input field like this:


// Enable checkbox
Field.enable('EXCESS_AMT0');
// uncheck that box
$('EXCESS_AMT0').checked = false;
// Disable checkbox
Field.disable('EXCESS_AMT0');
// Check that box
$('EXCESS_AMT0').checked = true;

If you find this post helpful, please click an ad below. Thanks!

4 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Comment by rc — 24-Oct-2009 #

    Short and sweet. Thnx!

  2. Comment by Andres Canavesi — 3-Nov-2009 #

    I didn’t know Field.disable, thanks :)

  3. Comment by Nick — 11-Nov-2009 #

    Wow, first try thanks!

  4. Comment by Nick — 11-Nov-2009 #

    When using a checkbox to trigger the check/uncheck event, remember to assign it an id and create an exception inside the function within toggleCheckBoxes (like if(item.id == ‘toggle_checkbox’) return; ) or else that checkbox will never change its checked status.

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress RSS XMLRSS Feed - Syndicate this Site and comments feed
linux GNUpowered by Apache tomcatMySQL ABPHP - The Language the Web Runs on

^Top^ Gare CalhounGare CalhounGare CalhounGare CalhounGare Calhoun