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

109
Vistas
JQuery $("body").append() not working on "first click"

I was trying to create a lightbox that triggers on a button click. I already have a lightbox that triggers on click on a div in the same document.ready that works perfectly, BUT the one on the button doesn't work the first time I click it. Then, works perfectly. Well, I don't want to create a tooltip that says "please click it twice first time", so how can I fixed? (If anyone can also explain to me why this happens would be marvelous)

$(document).ready(function () {
    //BUTTON CLASS
    $(".focosBTN").click(function () {
        $("body").append("<div class='img-popup'></div>");
        $(".img-popup").click(function () {
            $(".img-popup").fadeOut(500, function () {
                $(this).remove();
            }).addClass("lightboxfadeout");
        });
        $(".focosBTN").click(function () {
            $(".img-popup").fadeIn(500);
        });
    });
    //DIV CLASS
    $(".lightbox").click(function () {
        let imgsrc = $(this).find('.lightboxImg').attr('src');
        $("body").append("<div class='img-popup'><img src='" + imgsrc + "'></div>");
        $(".img-popup").click(function () {
            $(".img-popup").fadeOut(500, function () {
                $(this).remove();
            }).addClass("lightboxfadeout");
        });
    });
    $(".lightbox").click(function () {
                $(".img-popup").fadeIn(500);
    });
});
about 4 years ago · Santiago Trujillo
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