1

I have a add button which adds product id to the cart, which is handled by adding product id to the cookie separated by a pipe. Now on the cart page i have a remove button which should delete that particular product id from the cart which means the product id should be deleted from the cookie.

Ex: cookie value '22343|66562|88767'

when i cilck remove button on id 22343, the updated cookie value should be '66562|88767'

How can i achieve this using javascript.

V Raj
  • 11
  • 1
  • Hi look here https://stackoverflow.com/questions/2144386/how-to-delete-a-cookie – Bosco Jun 22 '19 at 18:41
  • Possible duplicate of [How to delete a cookie?](https://stackoverflow.com/questions/2144386/how-to-delete-a-cookie) – ehymel Jun 22 '19 at 19:00

2 Answers2

0

you can always update your cookie

document.cookie = "id=22343|66562|88767'"; // Create 'id' cookie
document.cookie = "id=66562|88767"; // Update the id cookie, i.e. overwrite

Further, if you want to delete, you expire the cookie

Read more at https://developer.mozilla.org/en-US/docs/Web/API/document/cookie

Bosco
  • 1,536
  • 2
  • 13
  • 25
TheWizardJS
  • 151
  • 9
0
function setCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "") + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    if (ca === null) return "";
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}


function eraseCookie(name) {
document.cookie = name + '=; Max-Age=-99999999;' }
Bosco
  • 1,536
  • 2
  • 13
  • 25
Cobysan
  • 99
  • 6