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

130
Views
Is there a way to toggle something out of localStorage?

I want to have a button that toggles something in and out of localStorage (it's clicked once and it adds something to localStorage, it's clicked again and that something disappears from localStorage). I want to do something like the code below, but toggling "status" instead of setting it permanently.

  function setStatus(statusName) {
    localStorage.setItem('status', statusName);
    document.documentElement.className = statusName;
  }

  (function () {
    if (localStorage.getItem('status')) {
      setStatus(localStorage.getItem('status'))
    }

This is code that I've used in something unrelated, and I know that it works. However, this sets the status, it doesn't toggle it. I'm wondering if there's a way to revise that code so that it does.

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

0

Maybe using Storage.removeItem()?

function toggleItem(item, value) {
  if (localStorage.getItem(item) {
    localStorage.removeItem(item);
  } else {
    localStorage.setItem(item, value);
  }
}

Does it satisfies your requirement?

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!