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

99
Views
Uso de almacenamiento local en botones Me gusta

Tengo un código que cambia de color cuando se hace clic en él:

 <button class="like" onclick="like(this)"><i class="fa fa-thumbs-up"></i></button>
 var state = true; function like(element){ if(state){ var currentElement = element.querySelector('.fa-thumbs-up'); currentElement.style.color = "orange"; }else{ var currentElement = element.querySelector('.fa-thumbs-up'); currentElement.style.color = "black"; } state = !state; }

Quiero guardar el color en el almacenamiento local: el usuario hace clic en Me gusta y le gusta hasta que no le gusta (no cuando actualizó).

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

0

Puede usar localSotrage.setItem(key, value) para almacenar los datos y localStorage.getItem(key) para recuperar los datos.

Para el caso que quisieras usar así

 var state = true; function like(element){ if(state){ var currentElement = element.querySelector('.fa-thumbs-up'); currentElement.style.color = "orange"; localStorage.setItem("storedColor", "orange"); }else{ var currentElement = element.querySelector('.fa-thumbs-up'); currentElement.style.color = "black"; localStorage.setItem("storedColor", "black"); } state = !state; } window.addEventListener('DOMContentLoaded', e => { currentElement.style.color = localStorage.getItem("storedColor"); });
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!