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

111
Vistas
How to make a simple fade in/out panel with jQuery?

I'm trying to make a text fade in and fade out, alternately, as you click a button, using jQuery. I came up with this code:

$("button").click(function() {
  $('#p1').fadeIn();

  $("button").click(function() {
    $('#p1').fadeOut();
    $('button').off("click");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button>Button</button>
<p id='p1'> Paragraph </p>

That works only once (fades in first click, fades out second) and the button stops working forever.

If I comment out line 5, the button works once and everytime I press it again, the text fades in and out at the same time, which is not what I want.

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Well, fadeToggle() is the way to go here.

But if I wanted to use only fadeIn and fadeOut for some reason... I solved it this way:

var clickCount = 0;
$('button').click(function() {
    if (clickCount % 2 == 0) {
    $('#p1').fadeIn();
    }
    else {
    $('#p1').fadeOut();
    }
    clickCount++;
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

There is a method fadeToggle() which toggles between fadeIn and fadeOut.

$("button").click(function() {
  $('#p1').fadeToggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<button>Button</button>
<p id="p1">Paragraph</p>

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