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

120
Vistas
JavaScript increase or decrease number by clicking

This my Html code:

<li>
<a href="#" class="likelink">
<span class="heart">
<i class="fa fa-heart-o fa-lg like"></i>
<i class=" fa fa-heart fa-lg like"> </i>
<span class="point">0</span>Like</span>
</a>
</li>

A heart icon with the number zero overlapping it

I didn't write css code

  $(".fa.fa-heart.like").css("display", "none");

If the user clicks on the letter or the heart, the zero should become one. If the user clicks on the text or the heart again, the one should become zero. How can I do that? I tried this but it didn't work.

This is my JS:

$(".fa.fa-heart.like").css("display", "none");

var like = parseInt($(".point").text()) + 1;
var flag = true;
var timeline1 = gsap.timeline({ repeatDelay: 0.1, paused: true });
timeline1.to(".likelink", { duration: 0.7, width: 50, ease: Back.easeIn })
    .to(".point", { duration: 0.2, opacity: 0, fontSize: 0 }, "-=0.7")//sayı
    .to(".fa.fa-heart-o.like", { duration: 0.4, display: "none" }, "-=0.5")//kalp
    .to(".fa.fa-heart.like", { duration: 0.1, display: "inline-block" }, "-=0.1")
    .to(".likelink", { duration: 0.7, width: 170 });

$(".likelink").click(function () {
    event.stopPropagation();
    flag ? timeline1.play() : timeline1.progress(0).pause();
    flag = !flag;
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You already have some code which gets the current number of likes: var like = parseInt($(".point").text()) + 1;

However, you're not currently doing anything with the like variable. You need to update the HTML with the updated value. You can do this by passing the like value into the same jQuery text method you're using to get the variable: $(".point").text(like);

You will want to do this within the click handler (i.e. the function which runs when the user clicks an element with the likelink class). However, at the moment you only ever change it once. You want to change it every time the element is clicked, so you need the code to be within that click handler.

You also want to track whether or not the user has already clicked the element. You have a flag variable which I assume you intend to use for this purpose, but you're not updating its value within the click handler. You need to negate it each time the user clicks. The current value of the flag should determine whether you add or subtract 1 to the like variable.

I've simplified your code to focus on the core problem, and added code in the click handler to update flag and display the new value of like.

$(".fa.fa-heart.like").css("display", "none");

var flag = false;
$(".likelink").click(function() {
  event.stopPropagation();

  flag = !flag;
  var like = parseInt($(".point").text());
  if (flag) {
    like += 1;
  } else {
    like -= 1;
  }
  $(".point").text(like);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li>
  <a href="#" class="likelink">
    <span class="heart">
      <i class="fa fa-heart-o fa-lg like"></i>
      <i class=" fa fa-heart fa-lg like"> </i>
      <span class="point">0</span>Like
    </span>
  </a>
</li>

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