Page 1 of 1

Checking checkboxes

Posted: Thu Aug 05, 2010 10:20 pm
by s.dot
Interesting how I've forgotten this. :(

I've got a form that allows for deletion of multiple comments.

In my HTML, it looks like this..

Code: Select all

<input type="checkbox" name="cdelete[18]">
<input type="checkbox" name="cdelete[19]">
<input type="checkbox" name="cdelete[20]">
How would I use javascript to check all of the cdelete[*] boxes?

I can do it fine if the name was just cdelete.. but then I couldn't process multiple checked items.

My current javascript looks like this:

Code: Select all

function checkAll()
{
	var cd = document.cdeleteform.cdelete;
	if (cd.length)
	{
		for (i=0; i<cd.length; i++)
		{
			cd[i].checked = true;
		}
	}
}

Re: Checking checkboxes

Posted: Thu Aug 05, 2010 11:04 pm
by superdezign

Code: Select all

function checkAll() {
  var cd = document.cdeleteform.elements["cdelete[]"];
  for (var i in cd) cd[i].checked = true;
}

Re: Checking checkboxes

Posted: Fri Aug 06, 2010 12:10 pm
by s.dot
This did not work.. I alert()'d "cd" and it comes up as undefined

Code: Select all

var cd = document.cdeleteform.elements["cdelete[]"];
alert(cd);
cdeleteform is the name of my form
cdelete is the name of the checkboxes although they have a number in them.. eg cdelete[18]

Re: Checking checkboxes

Posted: Fri Aug 06, 2010 12:14 pm
by s.dot
This works though..

Code: Select all

function checkAll()
{
	var cd = document.cdeleteform.elements;
	
	for (i=0; i<cd.length; i++)
	{
		if (cd[i].type == 'checkbox')
		{
			cd[i].checked = true;
		}
	}
}
Thanks for turning me on to the .elements part.. I was not aware of that.