Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Check for Checkbox is checked

The function always returns false, eventhough the checkbox is checked. I really couldn't crack down what i'm doing wrong. I'm using a checkbox to enable and disable the textbox in the gridview. However, it doesn't seem to work. Thanks for the help. I have posted the html and jq code below.

HTML code:

<asp:GridView ID="grdFees" runat="server" AllowPaging="false" CssClass="Grid" AutoGenerateColumns="false" EmptyDataText="No Data Found" EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-CssClass="gridItem" TabIndex="5">
<Columns>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="center"
                                ItemStyle-HorizontalAlign="center" ItemStyle-Width="2%">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkselect" runat="server" CssClass="checkbox" 
                                    Width="15px" Checked="false" />
                                </ItemTemplate>
                            </asp:TemplateField>

</Columns>
</asp:GridView>

Jquery code:

$(document).ready(function() 
    {
        $(".checkbox").click(function()
        {
        if ($(this).is(":checked")) 
        {
            alert("true");
        }else
        {
            alert("false");
        }
});
like image 252
Prince Avatar asked May 25 '12 10:05

Prince


People also ask

How do you check if a checkbox is checked?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

Which method is used to check the status of checkbox?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.

How do you checkbox is checked or not in jQuery on page load?

We can check the status of a checkbox by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') .


1 Answers

ASP.NET probably doesn't apply the value of CssClass to the check box itself, but to the generated label and/or container element.

Try using the :checkbox selector instead:

$(document).ready(function() {
    $("input:checkbox").click(function() {
        if ($(this).is(":checked")) {
            alert("true");
        } else {
            alert("false");
        }
    });
});
like image 72
Frédéric Hamidi Avatar answered Oct 20 '22 13:10

Frédéric Hamidi