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

108
Views
Using localstorage on like buttons

I have a code which is changing colour of thumb when it is clicked:

<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;
}

I want to save the colour in localstorage: User click on like and it is liked untill he dislike it (no when he refreshed).

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

0

You can use localSotrage.setItem(key, value) for storing the data and localStorage.getItem(key) to retreive the data.

For the case you wanted use like this


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!