Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery update cookie value

I am using jquery-cookie library to create cookie with JQuery. How can I update value of the cookie? I need it create new cookie and if the cookie exists to update it. How can I do this? Code that I got:

v.on('click', function(){
      var d = $(this).attr('role');       
      if(d == 'yes')
          {
           glas = 'koristan.'   
          }else {
              glas = 'nekoristan.'
          };
       text = 'Ovaj komentar vam je bio ' + glas;

       //This part here create cookie
       if(id_u == 0){
           $.cookie('010', id + '-' + d);
       }        
      $.post('<?php echo base_url() ?>rating/rat_counter', {id : id, vote : d, id_u : id_u}, function(){
         c.fadeOut('fast').empty().append('<p>' + text).hide().fadeIn('fast'); 
      });
    })
like image 677
Sasha Avatar asked Aug 10 '12 13:08

Sasha


People also ask

How to update the cookie value in jQuery?

To update a cookie all you need to do is create a cookie with the same name and a different value. To append your new value to the old... I need it to append new string to the old one.

How to clear cookie in jQuery?

To delete a cookie with JQuery, set the value to null: $. cookie("name", null, { path: '/' });

How check cookie is set or not in jQuery?

If this is the case, then you can actually check if a user has enabled cookies or not using the following straightforward jQuery snippet: $(document). ready(function() { var dt = new Date(); dt. setSeconds(dt.


1 Answers

To update a cookie all you need to do is create a cookie with the same name and a different value.

Edit

To append your new value to the old...

//Im not familiar with this library but 
//I assume this syntax gets the cookie value.
var oldCookieValue = $.cookie('010');  
//Create new cookie with same name and concatenate the old and new desired value.
$.cookie('010', oldCookieValue + "-" + id);
like image 198
marteljn Avatar answered Oct 14 '22 04:10

marteljn