Jabed Hossain Jabed Hossain - 6 months ago 41
jQuery Question

JQquery click event not working for html input(checkbox) inside asp panels

Inside an aspx file I have asp:panels under an UpdatePanel

ASPX:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Panel ID="PnlTraveler" runat="server" Visible="false">
<input id="chkBoxVisAst" type="checkbox" class="checkbox" enableviewstate="true" />
</asp:Panel>
<ContentTemplate>
</asp"UpdatePanel>


If I write my Jquery codes in the same page under the asp:Content head to work with the click event it doesn't recognize the event id in my case chkBoxVisAst => this id.

JQuery

asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">


<script type="text/javascript">

$(document).on('ready', function () {
$('#chkBoxVisAst').on('click', function ()
{
if ($(this).attr("checked") == "checked")
{
alert("checked");
} else {
alert("unchecked");
}
});
});
</script>


It works if I put my input(checkbox) outside my panel. How am I going to access my event id from inside the asp:panels?

Answer

The problem is in your code. You cannot add any element directly inside update panel. You have to put them in ContentTemplate.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="PnlTraveler" runat="server" Visible="false">
                    <input id="chkBoxVisAst" type="checkbox" class="checkbox" enableviewstate="true" />
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>

After adding the code above you will be able to access the checkbox element with its ID.

EDIT

Add the code below,

var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {
        $('#chkBoxVisAst').on('click', function () {
            if ($(this).attr("checked") == "checked") {
                alert("checked");
            } else {
                alert("unchecked");
            }
        });
    });

https://msdn.microsoft.com/en-us/library/bb311028(v=vs.100).aspx