Click to See Complete Forum and Search --> : checkboxes
lingra
04-21-2003, 04:50 PM
Hello,
I'm trying to use an array to loop thru checkboxes that are named, "job1","job2", "job3", etc. So far, I haven't figured out how to do it. I know the number of jobs. Any ideas? I'm wanting to check all or uncheck all. For other reasons, I can't name the all just "job" and use the inherent array.
khalidali63
04-21-2003, 05:13 PM
to create an array of checkboxes,nake them all the same,as may be
jobs,then using the form name go through them
cbarray = document.formName.jobs;
for(var x=0;x<cbarray.length;x++){
//and this is how you can check if its selected
if(cbarray[x].selected=="true"){
//selected message or processing
}
}
Hope this helps
Try something like this:
for (i=0; i<=4; i++)
{
document.formname.fieldname.job[i].selected = true;
}
lingra
04-21-2003, 05:17 PM
Unfortunitaly I can't name them all the same. So, jobs doesnt' work.
lingra
04-21-2003, 05:28 PM
When I try this
<script language="JavaScript">
function check() {
for (i=0; i<=4; i++) {
document.delform.job[i].checked = true;
}
return="Uncheck All";}
</script>
<body>
<form name=delform>
<INPUT type=checkbox name="job1" value=" 6" >
<INPUT type=checkbox name="job2" value=" xx" >
<INPUT type=checkbox name="job3" value=" xxx" >
<INPUT type=checkbox name="job1" value=" 123" >
<input type=button value="Check All "onClick="this.value=check()">
I get an error message saying that:
document.formname.job is null or not an object
You named two of the jobs the same thing. Is that all that's in your form? If so, you can just use elements[x] instead of job[x].
lingra
04-21-2003, 06:08 PM
No there is much more to the form but this is the pertinent to my problem. The last job should be job4 job... - unlimited number.
How do I use elements[] ?
Okay, if I could see your whole form I could make this more accurate. Say your form has 12 elements:
<html><head>
<script>
function check() {
for (i=7; i<=4; i++) {
document.frm.elements[i].checked = true;
}
return="Uncheck All";}
</script></head><body>
<form name="frm">
<input type=text>
<input type=text>
<input type=text>
<input type=text>
<input type=text>
<input type=text>
<input type=text>
<input type=text>
<input type=checkbox name="job1">
<input type=checkbox name="job2">
<input type=checkbox name="job3">
<input type=checkbox name="job4">
<input type=button value="okay" onclick="this.value=check()">
</form></body></html>
This would check all four checkboxes. Because it starts at the 7th element (the first checkbox) and ends at the last checkbox (second to last element, before the button).
Try that....
lingra
04-21-2003, 06:19 PM
the checkboxes are all mixed up with other type of elements.
How do you check to see if it is a check box and then check it or uncheck it?
All form elements can be referred to in the default array elements[]. Elements[0] is the first element inside of a form (the <form> tag). Elements[x], in this case, refers to all elements starting with the 7th one, and stopping at the 11th one.
Oh, I forgot to mention. Checkboxes don't work like most things. You can't use job1.checked=true; that doesn't work. You have to use return true; instead. Example:
<html><body>
<script>
function checkbox(theform){
for(x=0;x<=theform.elements.length-1;x++){
var boxColl=theform.elements[x];
if(boxColl.checked){boxColl.checked=false;}
else {boxColl.checked=true;}
} }
</script>
<form name='frm'>
<input type='checkbox' name='checkIt'><br>
<input type='checkbox' name='chk'><br>
<input type=checkbox name='hi'><br>
<input type=checkbox name="fish">
<br>
<input type='button' onclick="checkbox(this.form)" value="Click me!!">
</form></body></html>
lingra
04-21-2003, 06:58 PM
I think I finally got it.
Thanks alot,
Linda