-1

I have made a small shopping cart in html with javascript. You can put items in with the use of cookies. The problem is the delete button for a row in the shopping cart.

function cartShow(){
intTotalItems = 0;
intTotalItems = readCookie("totalItems"); 
tabelrow = "";

for (i = 1; i <= intTotalItems; i++){
    item = "item" + i;
    del = '<a href=""><img src="img/delete.png" onclick="delete1Cookie();" /> </a>';
    thisCookie = "";
    thisCookie = readCookie(item);
    fields = new Array();
    fields = thisCookie.split("|");
    tabelrow += "<tr>"
    + "<td>" + fields[0] + "</td>" 
    + "<td>" + fields[1] + "</td>" 
    + "<td>" + fields[2] + "</td>" 
    + "<td>" + fields[3] + "</td>" 
    + "<td>" + fields[4] + "</td>" 
    + "<td>" + fields[5] + "</td>" 
    + "<td>" + fields[4] * fields[5] + "</td>" 
    + "<td>" + del + "</td>" 
    + "</tr>";
}
document.write(tabelrow); }

function delete1Cookie(){
document.cookie = "item1=; 'empty'; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";}

When i use my function readCookie(item1); i have no problem. it shows the row of the product i want to delete.

But when i want to delete just one cookie my whole shopping cart turns empty.

My code is set to just one cookie for one line "item1" because i think i tried everything, so i would first like to see it work with just one specific row.

I already used the code from: https://stackoverflow.com/a/20156194/4912774 It deleted all my cookies:"item1 item2 etc".

Sorry for any mistakes this is my first post and english is not my native language

Community
  • 1
  • 1
Samichan
  • 13
  • 5
  • You can see a set of tested cookie utility functions here: http://stackoverflow.com/questions/20156129/add-cookies-to-input-background-color-change/20156194#20156194. – jfriend00 Sep 24 '15 at 08:24
  • Don't use cookies for this stuff. Use [web storage](http://www.w3.org/TR/webstorage/). It's [supported by all modern browsers](http://caniuse.com/#feat=namevalue-storage), doesn't make your HTTP requests bigger than they need to be, and unlike cookies, has a reasonable API. – T.J. Crowder Sep 24 '15 at 08:26
  • BTW, your code is falling prey to [*The Horror of Implicit Globals*](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html). Declare your variables. – T.J. Crowder Sep 24 '15 at 08:27
  • Lastly: Please search before posting. This question has been asked and answered several times. – T.J. Crowder Sep 24 '15 at 08:27
  • @jfriend00 that is the same setup i have. – Samichan Sep 24 '15 at 08:29
  • @T.J.Crowder I would love not to use any cookies, shame that it's part of an assignment. Also already searched but i only found how to delete one cookie. Not what to do if that doesn't work. – Samichan Sep 24 '15 at 08:31
  • Your code does not do the same thing in the example I linked to. – jfriend00 Sep 24 '15 at 08:35
  • @jfriend00 sorry i forgot to say that i tried a few different codes, also the function eraseCookie in your example. – Samichan Sep 24 '15 at 08:39

1 Answers1

0

Deleting a particular cookie , can be achived using to set it in past

function deleteCookie(cname) {
    var d = new Date();
    d.setTime(d.getTime() - (24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + "" + "; " + expires;
}
Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16
  • This was the code i also used, still didn't work. `function maakCookie(naam, waarde, dagen){ if (dagen){ var datum = new Date(); datum.setTime(datum.getTime()+(dagen*24*60*60*1000)); var verloopdatum = "; expires="+datum.toGMTString(); } else{ var verloopdatum = ""; } document.cookie = naam+"="+waarde+verloopdatum+";path=/"; }` – Samichan Sep 24 '15 at 08:35