June 3, 2010

How to fetch the value of selected checkbox inside a checkboxList?

Question by Starx

I want to know the selected value of the markup below. So that I can disabled a textbox, if one of the checkbox is selected.

    <asp:CheckBoxList ID="ChkTest" runat="server" RepeatDirection="Horizontal" CssClass="toggleYesNo">
        <asp:ListItem Value="1">Yes</asp:ListItem>
        <asp:ListItem Value="0">No</asp:ListItem>
    </asp:CheckBoxList>

I tried using this function it doesnot seem to work

$(document).ready(function() {
    $("#<%=ChkTest.ClientID %>").click(function() {
        value = $(this).val();
        if(value=='1') {
            $('#atextbox').attr('disabled','');
         }
         else {
            $('#atextbox').attr('disabled','disabled');
         }           

    });
});

I also track the output HTML but the id the CheckBoxList the assigned to a table instead.

UPDATED

<table id="ChkTest" class="toggleYesNo" border="0">
    <tr>
        <td><input id="ChkTest_0" type="checkbox" name="ChkTest$0" /><label for="ChkTest_0">Yes</label></td><td><input id="ChkTest_1" type="checkbox" name="ChkTest$1" /><label for="ChkTest_1">No</label></td>
    </tr>
</table>

Answer by Starx

Ok, I solved it

my jQuery function is

$(document).ready(function() {
    $("#<%=ChkTest.ClientID %> input").click(function() {
        value = $(this).attr('checked');
        if(value==true) {
            $("#TxtName").removeAttr("disabled");
        }
        else {
            $("#TxtName").val('');
            $("#TxtName").attr("disabled","disabled");
        }    
    });
});

This totally solved it

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!