title: jQuery checkbox change and click event
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
    //set initial state.
    $('#textbox1').val($(this).is(':checked'));
  
    $('#checkbox1').change(function() {
      $('#textbox1').val($(this).is(':checked'));
    });
  
    $('#checkbox1').click(function() {
      if (!$(this).is(':checked')) {
        return confirm("Are you sure?");
      }
    });
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;input type="checkbox" id="checkbox1"/&gt;&lt;br /&gt;
  &lt;input type="text" id="textbox1"/&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here <code>.change()</code> updates the textbox value with the checkbox status. I use <code>.click()</code> to confirm the action on uncheck. If the user selects cancel, the checkmark is restored but <code>.change()</code> fires before confirmation.</p>
  
  <p>This leaves things in an inconsistent state and the textbox says false when the checkbox is checked.</p>
  
  <p>How can I deal with the cancellation and keep textbox value consistent with the check state?</p>