/    Sign up×
Community /Pin to ProfileBookmark

checkboxes – how to check them when they’re dynamicaly named

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

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@pelegk1Jul 08.2003 — 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;
Copy linkTweet thisAlerts:
@requestcodeJul 08.2003 — 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>
Copy linkTweet thisAlerts:
@pyroJul 08.2003 — 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
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — 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
Copy linkTweet thisAlerts:
@requestcodeJul 08.2003 — 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?
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — 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...
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — Error: could not get the type property. This command is not supported.

Line 39 is where this occurs:

if(frmobj.elements[i].type="checkbox")
Copy linkTweet thisAlerts:
@requestcodeJul 08.2003 — Can you give a link to your code or post it here? The above example worked for me so possibly you left something out.
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — 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'>
Copy linkTweet thisAlerts:
@requestcodeJul 08.2003 — Try changing this: Type='Checkbox'

to this: Type='checkbox'

The "c" in should be lower case.
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — a slightly neater way of coding the dynamically named checkboxes is:

<INPUT Type='Checkbox' Name="<%= cbName %>" Value="<%= "YES" %>">

Amounts to the same thing.
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — nope - didn't work
Copy linkTweet thisAlerts:
@requestcodeJul 08.2003 — 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")
Copy linkTweet thisAlerts:
@idiotbearauthorJul 08.2003 — GET IN! it works!

Thanks - I shoulda spotted the == too! I guess I'm having a slow day ?
Copy linkTweet thisAlerts:
@requestcodeJul 08.2003 — Glad we figured that one out. Those sytax errors can be hard to find. Good Luck! ?
×

Success!

Help @idiotbear spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...