0

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);
      }
    },
  });
});
6
  • 3
    prop('checked', false) is correct. The issue is that this in the success handler function does not reference the .subareachk element which raised the event. Check the duplicate for more information and the solution. Commented Feb 3, 2020 at 10:50
  • 1
    since $(this).prop('checked', false); is inside the success callback, $(this) does not refers to checkbox. before the ajax call assign this to another variable, like var that = $(this), then change your $(this).prop('checked', false); to that.prop('checked', false); Commented Feb 3, 2020 at 10:52
  • @AkhilAravind thanks for replying .I will try now Commented Feb 3, 2020 at 10:53
  • 1
    @RoryMcCrossan Thank you it is working now Commented Feb 3, 2020 at 10:55
  • @AkhilAravind thank you it is working now Commented Feb 3, 2020 at 10:55

0

Browse other questions tagged or ask your own question.