Check single line checkboxlist

I am creating a table row that will automatically generate data from sql and bind internally. Is this a way to check if the user has clicked or not using javascript? The string is created like this:

<tr>
    <td align="right" class="formLine">Brand Type:</td>
    <td class="FormLine2" align="left" colspan="7">
        <asp:CheckBoxList ID="chkBrandType" runat="server" RepeatColumns="10" RepeatDirection="Horizontal" RepeatLayout="Flow" BorderWidth="0" />
    </td>
</tr>

      

+3


source to share


2 answers


JQuery solution

//assuming all your html elemnts and checkboxes are having unique ids
$("#check1").on('click', function(){
    if($(this).is(":checked")){
        alert("checked");
    else {
        alert("unchecked");
    }
}

      



Otherwise, if you just want to check if ANY checkbox was checked

$('input[type="checkbox"]').change(function(){
    if($(this).is(':checked')){
        alert('checked');
    } else {
        alert('unchecked');
    }
});

      

0


source


It's easy to do with the function below. Please note the usage CheckBoxList1.ClientID

to get the correct ID. Also you need to add input

to the listener as asp.net will create a table around the checkboxes.



<script type="text/javascript">
    $(document).ready(function () {
        $("#<%= CheckBoxList1.ClientID %> input").change(function (e) {
            if ($(this).prop("checked")) {
                alert($(this).val());
            }
        });
    });
</script>

      

0


source







All Articles