Using javascript to loop through a checkboxlist

N

nesster13

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)

//function
<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++)
if((chkList1).checked)
args.IsValid = true;
return;
}
</script>

//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:CheckBoxList>
<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
Response.Redirect("default.aspx")
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;
return;
}
</script>

OR

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

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
page.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top