Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
localstorage item value resetting

I have javascript code below

let beer_JSON = {"name": "beer", "price": 2.99, "quantity": 0};

let beer = document.getElementById("beer");

function alert(){
    Swal.fire({
         position: 'top-end',
         icon: 'success',
         title: 'added',
         showConfirmButton: false,
         timer: 1500
       });
 }

    beer.addEventListener("click", function() {
        alert();
        beer_JSON.quantity++;
        localStorage.setItem('beer', JSON.stringify(beer_JSON));
      });

My quantity value resets to 0 after page refreshes. How can I fix that

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This code executes every time page refreshes and resets quantity.

let beer_JSON = {"name": "beer", "price": 2.99, "quantity": 0};

You need to get this initial data from localStorage. Replace the first line with this code.

let beer_JSON = JSON.parse(localStorage.getItem("beer")) || {"name": "beer", "price": 2.99, "quantity": 0};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!