skip to Main Content

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


  1. Chosen as BEST ANSWER

    solution : thanks to @Sohail

    let color = Cookies.get("color");
    if (color) {
    $(".likes-button").toggleClass(color);
    }
    
    $(document).ready(function() {
          $('.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);
                        if (Cookies.get("color")) {
                        $(".likes-button").toggleClass(Cookies.get("color"));
                        Cookies.remove("color");
                        } else {
                        Cookies.set("color", "heart");
                        $(".likes-button").toggleClass("heart");
                        }
    
    
                    },
                    error: function(rs, e) {
                           alert(rs.responseText);
                    }
              });
        })
        });
    
    
    

  2. 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:

    if(Cookies.get('color')) {
     Cookies.remove('color');
    } else {
     Cookies.set('color', 'heart');
    }
    

    Full Code

    let color = Cookies.get("color");
    if (color) {
      $(".likes-button").toggleClass(color);
    }
    
    $(document).ready(function() {
      $(".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);
            if (Cookies.get("color")) {
              $(".likes-button").toggleClass(Cookies.get("color"));
              Cookies.remove("color");
            } else {
              Cookies.set("color", "heart");
              $(".likes-button").toggleClass("heart");
            }
          },
          error: function(rs, e) {
            alert(rs.responseText);
          }
        });
      });
    });
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search