[thelist] help me compress this JS function?

Tom Dell'Aringa pixelmech at yahoo.com
Thu Mar 27 15:29:21 CST 2003


Hey all,

I'm thinking I can slightly improve this function, but I'm not sure
where. Here is the function, which takes a group of radio buttons and
either selects them all or deselects them all, while changing the
name of the controlling button to reflect the method:
========================
function toggleAllCheckboxes(oForm, cbGroup, button)
{
	var len = oForm.length;
	for(i=0;i<len;i++)
	{
		currentElement = oForm.elements[i];
		if(oForm.elements[i].type == "checkbox")
		{
			var matchGroup = currentElement.value.substring(0,4);
			if(matchGroup == cbGroup)
			{
				if(currentElement.checked == true)
				{
					currentElement.checked = false;
					button.value = "Select All";
				}
				else
				{
					currentElement.checked = true;
					button.value = "Deselect All";
				}
			}
		}
	}
}
========================

There can be 2 or 3 groups of buttons like this on a page, which is
why I am doing the substring thing. I only need to name my CB groups
like "cbg1_somevalue" or "cbg2_anothervalue" to keep them distinct.

One thing I wanted to do was use a tertiary operator for the if then
else..but couldn't get it to work. This is what I tried: (pseudo)

if(matchGroup == cbGroup) ? uncheck boxes : check boxes

This throws a syntax error.. it seems to follow the right syntax but
obviously not. I don't have my Danny G book on me so I am lost :P

TIA

Tom

=====
http://www.pixelmech.com/
var me = tom.pixelmech.webDeveloper();

http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

"That's not art, that's just annoying." -- Squidward

__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com


More information about the thelist mailing list