I've been checking many and many posts here in stackoverflow but, even if many solutions should be working, it yet doesn't work for me.
I have a modal in bootstrap where I have a list of bootstrap checkboxes (<label><input> style).
Each checkbox has a set of data attributes that are directly setted from an AJAX request and, therefore, that aren't settet through jQuery.
In my situation, I need to remove all the data-attr from the checkbox when it is toggled and add some other.
The problem, however, is that I'm being unable to remove them.
I've first tried using jQuery.removeData(), but then I've read that it actually only removes data attributes if at least one of them has been set through jQuery, therefore it is not my case.
So, I've checked a little bit around and found out that I should be using jQuery.removeAttr() prototype and remove each data element.
So, my case is this one:
$('.modal_day_checkbox').on('change', function(){
if ($(this).is(':checked')) {
removeAllData($(this));
$(this).data('newid', 'test_id');
console.log($(this).data());
}
else {
removeAllData($(this));
$(this).data('testID', 'ID_NEW');
console.log($(this).data());
}
});
And, in order to have a clean code, I've implemented the function removeAllData:
function removeAllData(object) {
$.each(object.data(), function(i, v) {
object.removeAttr("data-"+i);
console.log('removing: '+'data-'+i);
});
}
Which is just looping through the object to check what data attributes it has and it should be removing it.
Surprisingly, the result is this one:

Any idea?
object.removeData()to ensure that both the cache and the HTML attributes are removed.removeData, which removes data from the internal jQuery data object ?.removeData()with no keys and get them all in one go.dataattributes causes the cache to be created, it's not just creation that does this.