Search code examples
jquerycheckboxcheckedunchecked

How can I click a checkbox and know if checked or not checked?


$('.p_check').click(function() {
          if ($(this).is(':checked')) {
              alert("checked");
          } else {
               alert("unchecked");
          }
       });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='checkbox p_check'><label><input type='checkbox'>New Password</label></div>

My alert is always "unchecked", no matter if I check or uncheck it


Solution

  • You are using div as element for checking, but you should use checkbox:

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