View Full Version : checking all checkboxes
ShMiL
04-17-2003, 11:48 AM
I use this function:
function CheckAll() {
var ml = document.messageList;
var len = ml.elements.length;
for (var i = 0; i < len; i++) {
var e = ml.elements[i];
if (e.name == "need2print") {e.checked = true;}
}
ml.toggleAll.checked = true;
}
in the form (name=messageList) I have checkboxes:
<input type="checkbox" name="need2print">
when I run this function I get an error: "elements" is null or not an object...
How is it possible? What can I do?
Thanks
david7777
04-17-2003, 12:50 PM
Try window.documents.messageList.need2print.checked = true;
ShMiL
04-17-2003, 01:46 PM
I made it simpler thinking it'll help me find the problem:
function CheckAll() {
for (var i = 0; i < document.messageList.need2print.length; i++) {
document.messageList.need2print[i].checked = true;
}
}
BTW - it didn't work with window before the document...
the error is now: "object unexpected"
any idea?
beetle
04-17-2003, 03:12 PM
How do you want to interact with this function? Another checkbox? A button? Let's try and make this function a bit flexible with a parameter or two, shall we?
Let me know you want to interface this thing and I'll get ya hooked up.
ShMiL
04-17-2003, 06:03 PM
I'm trying to do it with a checkbox: when it's checked - all checkboxes are cheked, when it's unchecked - all checkboxes aren't checked.
I copied the code from YahooMail but it didn't work, so I changed it a bit... and it didn't work either...
beetle
04-17-2003, 06:09 PM
Piece of cake (not to be confused with a piece of brothercake) :D<html>
<head>
<title>Test</title>
<script type="text/javascript">
function toggleChecks( control, groupName )
{
var cb, i = 0;
while ( cb = control.form.elements[groupName][i++] )
{
cb.checked = control.checked;
cb.onclick = function ()
{
if ( !this.checked && control.checked )
{
control.checked = false;
}
}
}
}
</script>
</head>
<body>
<form>
Check all <input type="checkbox" onclick="toggleChecks( this, 'test' );" />
<br />
<input type="checkbox" name="test" value="1" />
<input type="checkbox" name="test" value="2" />
<input type="checkbox" name="test" value="3" />
<input type="checkbox" name="test" value="4" />
<input type="checkbox" name="test" value="5" />
</form>
</body>
</html>
ShMiL
04-17-2003, 07:35 PM
can't figure out why this, and the Yahoo scripts works great - but NOT on my page!
I'm leaving this for the weekend - hoping I'll notice something I missed...
Thanks guys.
beetle
04-17-2003, 07:39 PM
What, my version isn't good enough? :(
It even unchecks the 'check all' checkbox when one in the group is unchecked!
ShMiL
04-17-2003, 07:48 PM
It works great on it's own...
there's something wrong with my code...
beetle
04-17-2003, 07:50 PM
Are all the checkboxes, including the control, in the same form? That would break it if they weren't
ShMiL
04-17-2003, 08:23 PM
they are - that was what I checked first. I don't know what is wrong...
I'll put your form and then add stuff on it one by one so I can find the prob...
I'll do it sunday or saturday.
Thanks!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.