Click to See Complete Forum and Search --> : checkboxes - how to check them when they're dynamicaly named
idiotbear
07-08-2003, 06:47 AM
hey guys
I have an ASP page which displays a recordset - each record in the rs has a dynamically named checkbox (ie. <%="InvoiceRun_" & rs.RecordID%>. It all works fine, except I have a little JS function that used to check all my checkboxes, back when my checkboxes were statically named:
function checkall(formname,checkname,thestate){
var el_collection=eval("document.forms."+formname+"."+checkname)
for (c=0;c<el_collection.length;c++)
el_collection[c].checked=thestate
}
I used to call this function like so:
<a href="javascript:checkall('approve', 'InvoiceRun',true)">Tick All</a>
What I'd like to know is whether anyone has any clue how to get it to check all the boxes now that they're not all called "InvoiceRun". They all still START with "InvoiceRun", but end in the recordID. Some kind of wildcard would be good, only my javascript doesn't run to that....
Cheers
R
pelegk1
07-08-2003, 06:56 AM
if i am not wrong u must first give all the id="InvoiceRun"
and for each one the name ->"InvoiceRun_"+i
for(i=0;i<InvoiceRun.length;++i)
document.all["InvoiceRun_"+i].checked=true;
requestcode
07-08-2003, 07:01 AM
Maybe this example will help:
<html>
<head>
<title>Check All</title>
<script language="JavaScript">
function docheck(formid,state)
{
frmobj = document.forms[formid];
len=frmobj.length
for(i=0;i<len;i++)
{
if(frmobj.elements[i].type="checkbox")
{
frmobj.elements[i].checked=state
}
}
}
</script>
</head>
<body>
<center>
<form name="myform">
<input type="checkbox" name="c0"> Checkbox One
<br>
<input type="checkbox" name="c2"> Checkbox Two
<br>
<input type="checkbox" name="c3"> Checkbox Three
<br>
<a href="javascript:docheck('myform',true)">Check All</a>
<br>
<a href="javascript:docheck('myform',false)">Un Check All</a>
</form>
</center>
</body>
</html>
There are two ways to reference checkboxes. One is the name, the other is using the forms elements array, something like this:
document.forms[0].elements[0]
That will return the first item (not necessarily a checkbox) in your form... To check what type of element it is, you use elements[0].type
idiotbear
07-08-2003, 08:24 AM
is there some way I could use IndexOf("InvoiceRun") in my function so that it just looks for any checkbox which has InvoiceRun in its name? (all my checkboxes do)
Rob
requestcode
07-08-2003, 08:28 AM
In the form do you have other checkbox's that are not named InvoiceRun_(some record id)? If not then the examples should work. If you do then what does the record id look like? Is it a number like InvoiceRun_00,InvoiceRun_01,etc?
idiotbear
07-08-2003, 08:33 AM
yep - all checkboxes are called InvoiceRun_00, InvoiceRun_346, InvoiceRun1212 etc.
I tried your example (obviously changing it where necessary to fit my form name etc) and it didn't work...
idiotbear
07-08-2003, 08:37 AM
Error: could not get the type property. This command is not supported.
Line 39 is where this occurs:
if(frmobj.elements[i].type="checkbox")
requestcode
07-08-2003, 08:40 AM
Can you give a link to your code or post it here? The above example worked for me so possibly you left something out.
idiotbear
07-08-2003, 09:02 AM
The form is called "approve".
here's the function in the head:
function docheck(formid,state)
{
frmobj = document.forms[formid];
len=frmobj.length
for(i=0;i<len;i++)
{
if(frmobj.elements[i].type="checkbox")
{
frmobj.elements[i].checked=state
}
}
}
In the body (ie. where it's called):
<a href="javascript:docheck('approve',true)">Tick All</a><br>
<a href="javascript:docheck('approve',false)">Untick
All</a>
My checkboxes, as I said, are created dynamically by the following ASP code:
<%
Response.Write _
"<INPUT Type='Checkbox' " _
& "Name='" & cbName & "' Value='YES'" & ">"
%>
Sample output of the above is this:
<INPUT Type='Checkbox' Name='InvoiceRun_328' Value='YES'>
requestcode
07-08-2003, 09:12 AM
Try changing this: Type='Checkbox'
to this: Type='checkbox'
The "c" in should be lower case.
idiotbear
07-08-2003, 09:12 AM
a slightly neater way of coding the dynamically named checkboxes is:
<INPUT Type='Checkbox' Name="<%= cbName %>" Value="<%= "YES" %>">
Amounts to the same thing.
idiotbear
07-08-2003, 09:15 AM
nope - didn't work
requestcode
07-08-2003, 09:44 AM
Ok, my fault. A stupid syntax error. On the if statement i only had 1 equals sign instead of the required two. What is strange is that it did work for me and did not return an error. The line should look like this:
if(frmobj.elements[i].type=="checkbox")
idiotbear
07-08-2003, 09:46 AM
GET IN! it works!
Thanks - I shoulda spotted the == too! I guess I'm having a slow day ;)
requestcode
07-08-2003, 09:52 AM
Glad we figured that one out. Those sytax errors can be hard to find. Good Luck! :D