i stored a button color in a cookie so that if the button clicked the css color stay after the page refresh, but i don’t know how to destroy the cookie after the second click so if the user click again the button the color back to the original , I using jquery cookie plugin
jquery :
var color = Cookies.get('color')
console.log(color)
if(color)
$('.likes-button').addClass(color)
Cookies.remove('color')
$('.likes-button').click(function(){
var csrftoken = $('[name="csrfmiddlewaretoken"]').val();
var el= this;
var ajaxdata = $(this).attr('data-id');
var ajaxstring = $(this).attr('string');
console.log(ajaxstring)
$.ajax({
type: "POST",
url: '/ajax/upvote/',
data: JSON.stringify({'product_id' : ajaxdata,'string': ajaxstring}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
"X-CSRFToken": csrftoken
},
success: function(response) {
var up = response['likes']
var string = response['string']
console.log(string);
console.log(up)
$(el).find('.target').html(up);
$(el).find('.string').html(string);
$('.likes-button').toggleClass(color)
Cookies.set('color', 'heart')
}
})
})
css
.heart i.fa-heart{
color : #f44336
}
2
Answers
solution : thanks to @Sohail
You could add an if block to check if the cookie is set or not.
If the cookie is already set then remove it, otherwise add it.
Example:
Full Code