-1

When getInfo() returns false, the window alert is thrown however the checkbox still gets changed; checked if unchecked and vice versa.

Any help?

$("input[type='checkbox']").click(function(event) {
  var someCondition = getInfo();
  if (someCondition === false) {
    event.preventDefault();
    window.alert("Nope");
    return false;
  }
});

function getInfo() {
  return false; // just for testing
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" />

5
  • 3
    I can't reproduce it: your code is working as expected. Also, avoid using alert() since it blocks script execution. Use console.log() and check your devtools console. Try console logging someCondition: are you sure it is evaluating to false? Commented Jan 31, 2019 at 10:01
  • 1
    I have placed your code in a snippet where it works absolutely fine. Check to ensure there's no errors in the console and that you're running your jQuery code after the DOM has loaded. Also ensure that getInfo() is returning false, not 'false' or 0. Commented Jan 31, 2019 at 10:03
  • This issue cannot be reproduced. Maybe you need to clear your cookies and cache and then try again. Commented Jan 31, 2019 at 10:09
  • Your code is working as you explained in the description. Commented Jan 31, 2019 at 10:22
  • Is getInfo() using an asynchronous call? Commented Jan 31, 2019 at 10:30

2 Answers 2

0

If i understand what you need correctly you could just use this

  $("input[type='checkbox']").click(function (event) {
         var someCondition = getInfo();
         if(someCondition === false) {
            var checkVal = $("input[type='checkbox']").val();
             $("input[type='checkbox']").attr( "checked", !checkVal );
             window.alert("Nope");

         }
     });

Or if you want checkbox to be un-checked if your condition is false

  $("input[type='checkbox']").click(function (event) {
         var someCondition = getInfo();
         if(someCondition === false) {
             $("input[type='checkbox']").attr( "checked", false);
             window.alert("Nope");

         }
     });

Hope it helps

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

1 Comment

Why would the OP need to make this more complicated than his existing working code?
0

Your code seems to work on fiddle, but some other plugin might be interfering with the behavior. Still, try the following and see if this solves the issue

 $("input[type='checkbox']").on('click',function (event) {
        var someCondition = getInfo();
        var checkBox = $(this);
        if(someCondition === false) { 
            event.preventDefault();
            checkBox.prop('checked', !checkBox.prop('checked'))
            window.alert("Nope");
            return false;
        }
    });

1 Comment

Why would the OP need to make this more complicated than his existing working code?

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.