I have been working with many checkboxes lately.
I came across this 'problem' with the .prevenDefault() click event and I tried to find a solution for this.
In my case I wanted to be able to decide if a checkbox could be checked/unchecked according to other fields. Sometimes I even had to open a dialog before the event fired.
This sounded easier than it turned out to be...
In this jsFiddle you can see what the problem is and how I tried to solve it (see code below as well). Most answers implied to use change instead of click. But than you can't use .preventdefault().
$('div').off('change','.wtf').on('change', '.wtf', function(e) {
//e.preventDefault();
//return false;
if($(this).prop('checked') == true) {
alert('I am true now, but I must stay false');
$(this).prop('checked', false);
}
else {
alert('I am false now, but I must stay true');
$(this).prop('checked', true);
}
});
Is this the best solution? Or is there any other way to make the checkbox wait untill it is allowed to change it's state?
As example: I would like the checkbox unchecked only if a user agrees with a message in a dialog by hitting 'OK'.