2

I have a checkboxlist control in one of the web application. I want to use javascript to handle the SelectedIndexChanged event.

The checkbox list is like

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem>One</asp:ListItem>
    <asp:ListItem>Two</asp:ListItem>
    <asp:ListItem>Three</asp:ListItem>
    <asp:ListItem>Four</asp:ListItem>
    <asp:ListItem>Five</asp:ListItem>
</asp:CheckBoxList> 

How can I get the SelectedIndexChanged event using javascript?

Joseph
  • 117,725
  • 30
  • 181
  • 234
MithunRaj
  • 103
  • 4
  • 12

2 Answers2

4

On server side.. put the follwoing..

CheckBoxList1.Attributes.Add("onclick", "ChangeCheckBox();");

In client side JavaScript section, implement the following function

function ChangeCheckBox() {}
Starx
  • 77,474
  • 47
  • 185
  • 261
Thit Lwin Oo
  • 3,388
  • 3
  • 20
  • 23
  • Yes, I was able to get the checkbox list event with this. Also there is a checkbox in my UI, so I am using the same onclick to get the checkbox event. But I am not getting this event when checkbox is selected. Should we use a different event for checkboxes? – MithunRaj Mar 22 '12 at 02:25
0

you can use below code

  document.getElementById('CheckBoxList1').onchange = function () {
                var input = document.getElementById('CheckBoxList1').getElementsByTagName("input")
                for (var i = 0; i < input.length; i++) {
                    if (input[i].type == "checkbox") {
                        if (input[i].checked == true) {
                            alert(input[i].value);//Get all the checked checkboxes
                        }
                    }
                }
            }
Neelam
  • 1,028
  • 12
  • 25