I have a span with a data attribute like this:
<span class="delete" data-action="enable" title="Non usare più questa anagrafica" data-toggle="tooltip" data-placement="top" >
On click on the span I seek for the data-action attribute to perform my actions on the backend and it works fine:
$('.delete').click(function(e){
e.preventDefault();
var clicked = $(this);
var toggle = $(this).children('i');
var id = $(this).closest('div').data('value');
var action = $(this).data('action');
If everything is ok I want to change the data-action to "disable" because this span is acting as an on/off button so I do:
if(action=='enable'){
toggle.removeClass('fa-toggle-off text-red').addClass('fa-toggle-on text-green');
toggle.parent('span').data('action','disable');
toggle.parent('span').attr('data-original-title','Non usare più questa anagrafica');
}else{
toggle.removeClass('fa-toggle-on text-green').addClass('fa-toggle-off text-red');
toggle.parent('span').attr('data-action','enable');
toggle.parent('span').attr('data-original-title','Riattiva questa anagrafica');
}
and if I inspect the element on the page I see that the data-action has been updated correctly.
The issue is that if I click on the button again console.log(action) will still return the original data-action value instead of the updated one (even if inspecting the element I see the updated one). What am I missing here? Action is re-set each time on click of the button so I don't expect it to be still the value from the previous iteration... also if I have a button already with action "disable" on page load my code works fine. Any hint?