-1

The first for loop updates the qty to user input

Hi, I'm fairly new to JS and I wanted to know how I can change/update the qty for the "inCart" under "productsInCart" so that it reflects the event.target.value from the user.

Right now, I'm able to get the qty value from the user but unable to reflect that change to the Local Storage. Thus, when I update the site, it does not appear to change the qty.

Liam
  • 27,717
  • 28
  • 128
  • 190
Paul Jung
  • 43
  • 1
  • 7

1 Answers1

1

You can update a local storage variable by using localStorage.setItem(key, value) with your key and value. Make sure to update the value before calling localStorage.setItem.

The Otterlord
  • 1,680
  • 10
  • 20
  • I tried to call ```localStorage.setItem(item.inCart, event.target.value); ```; however, it would just create a new key and set the value to the new key created. – Paul Jung Aug 14 '20 at 10:23
  • From what I can see of your code, it seems that you need to write to `"productsInCart"` and not `item.inCart`. I can't see all the code though, so forgive me if I'm wrong, but surely you just need to stringify `cartItems` and write to `"productsInCart"`, the same place you took the object from to start with? – The Otterlord Aug 14 '20 at 10:28
  • 1
    I was able to do ```localStorage.setItem("productsInCart", JSON.stringify(cartItems));``` and got it. Thank you! – Paul Jung Aug 14 '20 at 10:41