Forum Moderators: open

Message Too Old, No Replies

Check All Boxes

when served dynamically

         

digitalv

8:46 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anyone know of a way to automatically check/uncheck all checkboxes that appear in a form without having to specify the name of each checkbox?

The information on the screen I'm referring to comes from a database, and the checkbox name is the record ID in the DB so I have no way of building a static script that can check/uncheck these boxes since I won't know what the name is going to be.

jatar_k

8:54 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why not use whatever language you are using to pull the data from the db to dynamically write the javascript?

Bernard Marx

9:58 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function checkAllBoxes(form)
{
var elements = form.elements,
elm, k=0;
while(elm=elements[k++])
if(elm.type=='checkbox')
elm.checked = true;
}

digitalv

1:34 am on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bernard,

Thanks for the script but I'm having a bit of a problem with it. Actually the script you gave me works fine. I also made a second function to UNcheck the boxes, the only difference being the function name and the value to "false". Both of these work fine when I run them as is.

The problem is that I'm trying to make a "master" checkbox where I can check/uncheck them all by checking/unchecking one box. I accomplished this with this function:

function DoTheCheck() {
if(document.form1.checkuncheckall.checked == true)
checkAllBoxes(form1)
else
uncheckAllBoxes(form1)
}

And I call it like this:
<input type="checkbox" name="checkuncheckall" value="1" onClick="Javascript:DoTheCheck()">

The form name is "form1" as you probably guessed. This script works fine in Internet Explorer, but doesn't do anything at all in Firefox. Both the function you gave me and the version of it I made to uncheck work fine when called alone in both browsers, but I'm not sure why the DoTheCheck only seems to work in internet explorer. Any ideas?

Rambo Tribble

3:08 am on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps FireFox doesn't equate a value of "1" with a value of "checked".

Rambo Tribble

4:14 am on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a minor variation on Mr. Marx's work that flies on Mozilla, at least:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function boxCheck(frm,stat){
var frm_el=frm.elements;
var frm_ln=frm_el.length;
for(var i=0;i<frm_ln;i++){
if(frm_el[i].type=="checkbox")frm_el[i].checked=stat;
}
}
</script>
</head>
<body>
<form action="">
<input type="checkbox" />Box 1<br />
<input type="checkbox" />Box 2<br />
<input type="checkbox" />Box 3<br />
<input type="checkbox" />Box 4<br />
<input type="checkbox" />Box 5<br /><br />
<input type="checkbox"
onclick="var x=false;if(this.checked==true)x=true;boxCheck(this.form,x);" />Master Control
</form>
</body>
</html>

Rambo Tribble

12:40 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't know what I was thinking last night; for better code efficiency change the onclick to simply:

onclick="boxCheck(this.form,this.checked);"

digitalv

7:48 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great, that did the trick thanks!