Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda