-1

jQuery code:

if($(this).attr('data-status') == 'Yes'){
    alert('if');
    $(this).attr("data-status","No");
}
else{
    alert('else');
    $(this).attr("data-status","Yes");
}

jQuery function code:

$(".Notification").click(function(){
  var notification = {
    petid: $(this).attr('data-value'),
    status: $(this).attr('data-status')
  }
  var formurl = '<?php echo base_url();?>index.php/Dashboard/notification';
  $.ajax({
    method: "POST",
    url: formurl,
    data: notification,
    success: function(response){
        if($(this).attr('data-status') == 'Yes'){
            alert('if');
            $(this).attr("data-status","No");
        }
        else{
            alert('else');
            $(this).attr("data-status","Yes");
        }
    }
  })
})

HTML Code:

<input type="checkbox" class="Notification" data-value="6" data-status="Yes" checked="">

I am trying to update the value of data-status but it not working. It alerting me if or else. but not updating value.

1 Answers1

1

$(this) will not refer to your element inside the success so define $(this) at the top of the click event.

The change event should be used for checkboxes (since a click is not needed to trigger a checkbox change event)

$(".Notification").on("change", function() {

  var ThisIt = $(this);

  var notification = {
    petid: ThisIt.attr('data-value'),
    status: ThisIt.attr('data-status')
  }

  var formurl = '<?php echo base_url();?>index.php/Dashboard/notification';

  $.ajax({
    method: "POST",
    url: formurl,
    data: notification,
    success: function(response){
        if(ThisIt.attr('data-status') == 'Yes'){
            alert('if');
            ThisIt.attr("data-status","No");
        }
        else{
            alert('else');
            ThisIt.attr("data-status","Yes");
        }
    }
  })
})

for more information you can take a look at : https://stackoverflow.com/a/6394826/3385827

Community
  • 1
  • 1
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28