0
I am using the following function to change the status of the Active to Inactive registration and vice versa, the first click works perfectly, but when the data is returned to the button the code does not recognize the new values and keeps sending the data of the previous situation.
Button Code:
<button data-status="1" class="btn btn-sm btn-success text-uppercase js-cadastro-status ">Ativo</button>
Code Executed:
$('.js-cadastro-status').on('click', function(event) {
event.preventDefault();
var $this = $(this);
var $cadastro_status = $this.data('status');
var $cadastro_id = $this.closest('.item').data('id');
var $bg = ($cadastro_status == 1) ? "bg-success" : "bg-danger";
var $remove_class = ($cadastro_status == 1) ? "btn-success" : "btn-danger";
var $add_class = ($cadastro_status == 1) ? "btn-danger" : "btn-success";
var $texto = ($cadastro_status == 1) ? "inativo" : "ativo";
$.ajax({
dataType: "JSON",
type: "POST",
url: path + "api/cadastros.php",
data: {
action:"fc-status-cadastro",
cadastro_id:$cadastro_id,
cadastro_status:$cadastro_status
},
beforeSend: function(data){
$this.LoadingOverlay("show",{backgroundClass:$bg});
},
success: function(retorno){
$this.LoadingOverlay("hide");
$this.removeClass($remove_class).addClass($add_class).text($texto).attr('data-status', retorno.status);
},
error: function(jqXHR, exception){
console.log(GetError(jqXHR, exception,'alert-hide'));
}
});
});
In the row below I change the classes and attribute data-status
button, the new attribute value data-status
is not recognized in the new click, thus passing the old value of the situation.
$this.removeClass($remove_class).addClass($add_class).text($texto).attr('data-status', retorno.status);
I have tried to change the code by leaving the beginning the following way, but it didn’t work either:
$('.js-cadastro-status').on('click', '.js-cadastro-status', function(event) {...}
your return.status is returning correct, you have this code?
– Jhoweb - Jonathan Gibim
Yes, but I ended up changing the code rule so I didn’t use the date attribute and using attributes named only with
status
. So I researched manipulating thedata
is a little more complex and as it is a small project I decided to change.– Helison Santos
Look, I believe that you are returning the data via PHP, what do you think of creating this button inside an if and Else php, and in the attributes of the button, vc put an onclick="Return alterr_status(id, value_status);Return false;" then in return ajax, you see if you update the page or then you replace a new onclick in the button attribute, I would do so
– Alisson Maciel