I need to uncheck a dynamically created checkbox. When the checkbox is clicked I need to see whether the value is already present in the database or. If present, then I want to uncheck the checkbox. I tried different ways, but nothing is working.
$(document).on('change', '.subareachk', function() {
var id = $(this).val();
$.ajax({
url: '{!! url('validatesubarea') !!}',
type: 'get',
data: {
'id': id,
},
dataType: 'json',
success: function(data) {
if (data.length > 0) {
alert('Already a Store has been assigned for this sub area');
$(this).prop('checked', false);
}
},
});
});
prop('checked', false)
is correct. The issue is thatthis
in thesuccess
handler function does not reference the.subareachk
element which raised the event. Check the duplicate for more information and the solution.$(this).prop('checked', false);
is inside the success callback,$(this)
does not refers to checkbox. before the ajax call assign this to another variable, likevar that = $(this)
, then change your$(this).prop('checked', false);
tothat.prop('checked', false);