Using javascript to loop through a checkboxlist

Discussion in 'Javascript' started by nesster13@gmail.com, Oct 14, 2006.

  1. Guest

    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
    , Oct 14, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wjer
    Replies:
    0
    Views:
    2,022
  2. JD
    Replies:
    5
    Views:
    11,638
    NetArchitect
    Aug 8, 2007
  3. Patrick.O.Ige
    Replies:
    5
    Views:
    19,100
    Patrick.O.Ige
    Jun 19, 2005
  4. Roedy Green
    Replies:
    3
    Views:
    424
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    365
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page