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" />
alert()since it blocks script execution. Useconsole.log()and check your devtools console. Try console loggingsomeCondition: are you sure it is evaluating tofalse?getInfo()is returningfalse, not'false'or0.getInfo()using an asynchronous call?