I am trying to uncheck input checkbox in jquery when i click No button of modal box and check it I click Yes button .
$('body').off('change', '[name="techOptions"][value="8"]').on('change', '[name="techOptions"][value="8"]', function() {
if ($(this).is(':checked')) {
var $n = noty({
text: 'Please provide relevant complementary information about .....',
modal: true,
layout: 'center',
type: 'neutral',
buttons: [{
addClass: 'btn btn-default',
text: 'Ok',
onClick: function($noty) {
$(this).prop("checked","checked");
$noty.close();
}
},
{
addClass: 'btn btn-default',
text: 'No',
onClick: function ($noty) {
$(this).removeAttr('checked');
$noty.close();
}
}]
});
// Focus on "OK" button
$n.$buttons.find('#button-0').first().focus();
return false;
}
});
I can see in developer tool the checked is added(checked = "checked") when clicking yes and hidden when clicking no, but in the UI the checking mark is not updating.
I tried $(this).prop("checked","checked");
and $(this).prop("checked",true);
and also $(this).attr("checked",true);
in Yes button, but without success .
2
Answers
To uncheck an input checkbox using jQuery, you can use the
prop()
function and set thechecked
property tofalse
. Here’s the updated code for the "Yes" button click handler:By setting
$(this).prop("checked", false)
, you are unchecking the checkbox associated with the current context.Make sure that the
$(this)
refers to the correct element in your context. If not, you might need to store the reference to the checkbox element in a variable and use that variable within the button click handler.Similarly, for the "No" button, you don’t need to remove the
checked
attribute. Instead, set thechecked
property totrue
to check the checkbox. Here’s the updated code:By making these changes, the checkbox should update its checked state in the UI when you click the "Yes" or "No" button within the modal box.
This code work for your case