0
<asp:CheckBox runat="server" ID="checkbox1" />   

Whenever I check or uncheck my asp checkbox, it needs to trigger an event in javascript but it seems like the .click function is not triggered. Is the .click event wrong?

$(document).ready(function () {
    $("#checkbox1").click(function (e) {
        if (this.checked) {
            //do something
        }
        else {

        }
    });
});
1
  • check if you are using master page then id checkbox1 will not work, you have to give the element some class and in javascript use that class selector. Commented Jan 14, 2017 at 9:43

2 Answers 2

1

You probably need to change

$("#checkbox1").click(function (e) {

into

$("#<%= checkbox1.ClientID %>").click(function (e) {

asp.net renamed the ID's of Controls to ensure there are no duplicates. If you check the HTML source the could look something like this: ContentPlaceHolder1_checkbox1. That is why jQuery cannot find it.

Sign up to request clarification or add additional context in comments.

Comments

0

I hope it will work for you. For dynamically created element you have to use event delegation, if you want to select by an attribute you can use an attribute equals selector.

$(document).on("change", "input[name='member']", function () {
              alert("CheckBox Changed.");
              if (this.checked) {alert("CheckBox checked.");}
              else{alert("CheckBox not checked.");}
          });

OR

$(document).on("change", "$('#<%=checkbox1.ClientID%>')", function () {
              alert("CheckBox Changed.");
              if (this.checked) {alert("CheckBox checked.");}
              else{alert("CheckBox not checked.");}
          });

Here is the link , and go through this link JS Fiddler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.