Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Using javascript to loop through a checkboxlist

Thread Tools

Using javascript to loop through a checkboxlist
Posts: n/a
Thanks in advance for any suggestion.
Currently I have an .aspx page that have a checkboxlist and a button.
What I would like to do is once the user click the button, the validate
function will fire and validate to make sure that at least 1 check box
is selected in the checkbox list.
Here is what i currently have: (I think my syntax for looping and
checking is wrong)

<script language="javascript">
function validate(source, args)
args.IsValid = false;
var chkList1 = document.getElementById ("CheckBoxList1");
var arrayOfCheckBoxes = chkList1.getElementsByTagName("input");
for(var i=0;i<arrayOfCheckBoxes.length;i++)
args.IsValid = true;

//checkboxlist and button control
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="0">Item1</asp:ListItem>
<asp:ListItem Value="1">Item2</asp:ListItem>
<asp:ListItem Value="2">Item3</asp:ListItem>
<asp:CustomValidator ID="CustomValidator1"
ClientValidationFunction="validate" ErrorMessage="Please check at least
one check box!!!" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" />

//button Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles_ Button1.Click
If Page.IsValid Then
End If
End Sub

I was able to fire the function because when I clear out everything in
the function to

<script language="javascript">
function validate(source, args)
args.IsValid = false;


<script language="javascript">
function validate(source, args)
args.IsValid = false;

it will act accordingly. With the first block it will display the error
message and for the second block it will redirect to the default.aspx

What I would like to know is does anybody has the javascript code to
loop through the checkboxlist and check to see if an item is check or
not? I plan to set the args as false and will set it to true if any
check box is checked.

Thank you

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Checkboxlist inside Repeater - doesn't fire click event for Checkboxlist JD ASP .Net 5 08-08-2007 11:28 AM
How to get Selected item in a Databinded CheckBoxList when CheckBoxlist is in a DataList? Patrick.O.Ige ASP .Net 5 06-19-2005 06:11 AM
Way to loop code for inserting Checkboxlist values into DB? wjer ASP .Net 0 02-13-2004 08:39 PM