merch89 merch89 - 1 month ago 14
Javascript Question

CheckBox Jquery - Prompt Not Working

I had this working previously but for some reason it has stopped working, a syntax error I presume. Can anyone spot the issue?

I have had it running with the html inserted into the source on a browser and Jquery in a console. But not in JSFiddle and a few other options.

It should prompt when ticked and prompt again if unticked:

<td class="icon">
<input type="checkbox" id="my_checkbox">
</td>


$("#my_checkbox").click(function() {
if (this.checked) {
this.checked = confirm('Are you sure you want to CONFIRM the order and EMAIL THE CUSTOMER?');
}
else {
this.checked = !confirm('Do you really want to change this to NOT RECEIVED?');
}
});


https://jsfiddle.net/thdbfuhs/

Answer

without jquery

$("#my_checkbox").click(function() {
  if (document.getElementById('my_checkbox').checked) {
    this.checked = confirm('Are you sure you want to CONFIRM the order and EMAIL THE CUSTOMER?');
  }    
  else {
    this.checked = !confirm('Do you really want to change this to NOT RECEIVED?');
  }  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td class="icon">
        <input type="checkbox" id="my_checkbox">
</td>

with jquery

$("#my_checkbox").click(function(e) {
  if ($(this).is(':checked')) {
    this.checked = confirm('Are you sure you want to CONFIRM the order and EMAIL THE CUSTOMER?');
   
  }    
  else {
    
     e.preventDefault();
  }  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td class="icon">
        <input type="checkbox" id="my_checkbox">
</td>