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

83
Views
How to add a like to something but only once using local storage?

I'm working with an API and i need to display movies and like them but i need to like them once. I tried to add a boolean and I had something like this:

let beenLiked = false;

const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
    console.log("ok like")
    axios.patch(`myUrl`)
    beenLiked = true
}, true)

the problem is when I like a movie, i can't like another one because of my boolean, how can I modify it to solve my problem?

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

0

If you can get the name of the movie that is supposed to be liked (I don't have your html file to do it for you), you can track the names and do it like this:

let beenLiked = []

const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
    const movie = "The Movie" // Get the movie name or id which has to be liked
    if(beenLiked.indexOf(movie) === -1){
        console.log("ok like")
        axios.patch(`myUrl`)
        beenLiked.push(movie)
        return true
    }
    console.log("Already liked once")
    return false
})

here the simplified version without html:

let beenLiked = []

function a() {
  const movie = "The Movie" // Get the movie the likeButton is attached to
  if (beenLiked.indexOf(movie) === -1) {
    beenLiked.push(movie)
    return true
  }
  return false
}
console.log(a())
console.log(a())

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!