html - Jquery checkbox .prop click preventDefault behaviour -


i have been working many checkboxes lately. came across 'problem' .prevendefault() click event , tried find solution this. in case wanted able decide if checkbox checked/unchecked according other fields. had open dialog before event fired. sounded easier turned out be...

in this jsfiddle can see problem , how tried solve (see code below well). answers implied use change instead of click. can't use .preventdefault().

$('div').off('change','.wtf').on('change', '.wtf', function(e) {     //e.preventdefault();     //return false;     if($(this).prop('checked') == true) {         alert('i true now, must stay false');         $(this).prop('checked', false);     }     else {         alert('i false now, must stay true');         $(this).prop('checked', true);     } }); 

is best solution? or there other way make checkbox wait untill allowed change it's state?

as example: checkbox unchecked if user agrees message in dialog hitting 'ok'.

checkboxes special case change event , click can replace each-other since change event fired clicking on checkbox.

that said big difference between click , change usability of .preventdefault(). change event better in cases value of checkbox being changed using other methods clicking.

in case choose whichever prefer. example be: fiddle here

$('input[type="checkbox"]').on('change', function () {     var ch = $(this), c;     if (ch.is(':checked')) {         ch.prop('checked', false);         c = confirm('do you?');         if (c) {             ch.prop('checked', true);         }     } else {         ch.prop('checked', true);         c = confirm('are sure?');         if (c) {             ch.prop('checked', false);         }     } }); 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -