asp.net - Javascript Confirmation for CheckBox on Unchecked Click -
i have checkbox auto post when changed.
the auto post works fine both (checked , unchecked), want popup dialog box confirm before each event takes place.
so far popup box works when checkbox checked.
but not popup confirmation dialog box, when checkbox unchecked.
question: how popup dialog box uncheck event using client side code (only)
<asp:checkbox id="currentcheckbox" runat="server" autopostback="true" checked='<%# bind("bdvalue") %>' oncheckedchanged="sharedfunctionforthischeckbox_checkedchanged" onclick="checkboxconfirmclick(this);" /> <script type="text/javascript"> function checkboxconfirmclick(elementref) { if (elementref.checked) { if (window.confirm('are sure?') == false) elementref.checked = false; } } </script>
if current method works can simplify to
function checkboxconfirmclick(elementref) { if (!window.confirm('are sure?')) { // if not sure elementref.checked = !elementref.checked; // toggle } }
however, best pass event through handler instead (i.e. e
) , do
function checkboxconfirmclick(e) { if (!window.confirm('are sure?')) { // if not sure e.preventdefault(); // kill click } }
you may want e.stoppropagation()
if have other handlers attached higher don't want event reach.
Comments
Post a Comment