I have a group of radio buttons...
<input type="radio" name="option1" value="foo">
<input type="radio" name="option1" value="bar">
that when checked will use a confirm() to ask if the user really wants to check that radio button or cancel. If user cancels I want the radio button to be unchecked so that user can repeat the same process if necessary:
$("input [type=radio]").change(function() {
if(confirm_canceled) {
$(this).prop("checked", false).trigger('change');
}
});
The radio button is unchecked but if I click the radio button again the event handler isn't activated. I guess it's because change() triggers on changes on the value and nothing has changed, so how do I fix this?
edit: issue was a bug caused by using confirm instead of confirm_canceled * face palm *
Here is a basic example.
$('input[type=radio]').on('change', function () {
var checked = confirm('Are you sure?');
if (!checked) {
$(this).prop('checked', false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="option1" value="foo">
<input type="radio" name="option1" value="bar">
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments