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

134
Views
Using one input form and using localStorage on 2 buttons

I've tried to use different class names/key names it still won't pull any data to localStorage for the liked2.on("click", saveData).. do I need to incorporate a for loop of some sort so it passes through both buttons?

** note the local storage does work on the liked1.on("click", saveData) **

        <form id="form1">
          <label>
            <h4>Destination One</h4>
            <input
              type="text"
              name="cityName"
              placeholder="Enter a destination"
              id="input"
            />
          </label>
          <button type="submit" class="primary button search-button">
            search
          </button>
          <div type="button" class="button button-like" id="liked1">
            <i class="fa fa-heart"></i>
          </div>
        </form>

  function saveData() {
    var cities = document.getElementById("input").value;
    localStorage.setItem("list", JSON.stringify(cities));
    var savedCity = JSON.parse(localStorage.getItem("list"));

    localStorage.setItem("list", JSON.stringify(savedCity));
    var result = JSON.parse(localStorage.getItem("list"));
    console.log(result);
  }

  liked1.on("click", saveData);
  liked2.on("click", saveData);

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

0

I can't clearly understand what do you want to do, so I will try my best to answer your question.

Every time you use setItem with the same keyName, it overrides the old one and fill it with the new one.

So you can

  • Use different keyNames for each form, for example, form1_cityName for the first form and form2_cityName for the second form
  • Change the format you save data in setItem, like JSON
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!