Click here to Skip to main content
15,672,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i tried my best but when i click on add to cart button it will not changed to my expectation

What I have tried:

JavaScript
$(".cart").click(function () {
    var idstr = this.id.toString();
    console.log(idstr);
    if (cart[idstr] != undefined) {
      console.log("exist");
      qty = cart[idstr][0] + 1;
      console.log(qty);
      console.log(cart[idstr][0]);
    } else {
      qty = 1;
      price = document.getElementById("price" + idstr).innerHTML;
      image = document.getElementById("img" + idstr).src;
      aname = document.getElementById("name" + idstr).innerHTML;
      console.log("not");
      cart[idstr] = [qty, aname, image, price];
    }

    console.log(cart);

  updateCart(cart);

    localStorage.setItem("cart", JSON.stringify(cart));
  });

  function updateCart(cart) {
    for (var item in cart) {
      document.getElementById("div" + item).innerHTML =
        "<button id='minus" +
        item +
        "' class='btn btn-primary minus'>-</button> <span id='val" +
        item +
        "''>" +
        cart[item][0] +
        "</span> <button id='plus" +
        item +
        "' class ='btn btn-primary plus'>+</button>";
    }
  }
Posted
Updated 3-Feb-23 12:29pm
v2

1 solution

Important Note :
Try to arrange your code by using (
JavaScript
PUT-YOUR-CODE-HERE
) or use pastebin site to arrange your code

To get help quickly, Also explain your issue in more details as possible as you can

hope you find what you are looking for
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900