PDA

View Full Version : check specified checkboxes


shibby1011ph
10-13-2002, 03:37 AM
for example:

i have 5 checkboxes: checkbox1, checkbox2, until checkbox5.
when i check checkbox1, i would like all odd checkboxes to be checked. so that means, checkbox3 and 5 should be checked. same goes for checkbox2, all even checkboxes should be checked.

how can i do this in javascript? really need your help!!!

chrismiceli
10-13-2002, 03:47 AM
yes you can


function even() {
document.formname.even1.checked = true;
document.formname.even2.checked = true;
etc.
}
function odd() {
document.formname.checkbox1.odd1.checked = true;
etc;
}


<form name="formname">
<input type="checkbox" name="even1" onClick="even()">
<input type="checkbox" name="odd1" onClick="odd()">
<input type="checkbox" name="even2" onClick="even()">
</form>

it can probably be done easier but that will do you.

adios
10-13-2002, 05:28 AM
I have no idea why this works ;).


<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function alternate(box) {
var el, e = 0, box_is_odd = box.name.charAt(box.name.length-1) % 2;
while (el = box.form.elements[e++])
if (el.type == 'checkbox' && el.name && el.name.indexOf('checkbox') != -1)
if (box_is_odd == el.name.charAt(el.name.length-1) % 2) el.checked = box.checked;
}

</script>
</head>
<body onload="document.forms[0].reset()">
<form>
<font color="green">
<input name="checkbox1" type="checkbox" onclick="alternate(this)"> checkbox <b>1</b></font><br>
<font color="red">
<input name="checkbox2" type="checkbox" onclick="alternate(this)"> checkbox <b>2</b></font><br>
<font color="green">
<input name="checkbox3" type="checkbox" onclick="alternate(this)"> checkbox <b>3</b></font><br>
<font color="red">
<input name="checkbox4" type="checkbox" onclick="alternate(this)"> checkbox <b>4</b></font><br>
<font color="green">
<input name="checkbox5" type="checkbox" onclick="alternate(this)"> checkbox <b>5</b></font><br>
</form>
</body>
</html>


Second possible scenario:

change this:

if (box_is_odd == el.name.charAt(el.name.length-1) % 2) el.checked = box.checked;

...to this:

el.checked = (box_is_odd == el.name.charAt(el.name.length-1) % 2) ? box.checked : false;

...to un-check the alternate boxes as well.

shibby1011ph
10-14-2002, 04:54 AM
hi! i just want to say thank you to everyone who helped me. i just have another dilemma though.

the code works but it takes too long. it takes 4 seconds for the other checkboxes to be checked. is this because of my pc or because of the code? my pc is pentium3 with 128 ram.

glenngv
10-14-2002, 05:02 AM
it loops thru all the form elements and look for checkbox. maybe you have bunch of form elements

adios
10-14-2002, 05:10 AM
4 seconds is 4 years in computer time. Something else sounds amiss; hard to diagnose without seeing your page.

shibby1011ph
10-15-2002, 06:01 AM
i have 1000+ elements... is that perhaps the problem?

adios
10-15-2002, 07:04 AM
¡¡¡¡ :eek: !!!!!

glenngv
10-15-2002, 07:05 AM
modified version of adios' code :)


function alternate(box) {
var el, e = 0, box_is_odd = box.name.charAt(box.name.length-1) % 2;
for (var i=1;i<=5;i++){
el = box.form.elements["checkbox"+i];
if (box_is_odd == i % 2) el.checked = box.checked;
}
}