Click event only works the first time

Asked

Viewed 853 times

0

I’ve looked for it in other places but I couldn’t find a solution that would work for me. This is the site:

http://pedrowebdesign.hol.es/damaso/

These 3 squares at the top, are locations for photos of the services provided to the last 3 customers (presented in slideshow). When I click on any of them, Javascript performs an AJAX call to a PHP script that lists the images of the inserted folders as 'data' attribute in the link. Each client has an image folder. Yes, the site is static. I made a homemade lightbox to not use too many plugins (since I’m using jcycle). The problem is when I close the lightbox.

Whoever is reading, please visit the site (not yet optimized for smartphones and tablets) and click one of the blue boxes at the top. It worked, didn’t it? Now click on the little square in the upper right corner. You may notice that everything closes as expected. Now try one more click on any of the boxes. Very likely it didn’t work. I couldn’t identify the error so far. Could anyone tell me why this is happening? It only works on the first click. Then, just reload the page :/

Here’s the code:

<script type="text/javascript">
$(document).ready(function(){
    $('.featured .client').on("click", function(e){
        e.preventDefault();
        $('body').addClass('active');
        $('.modal').fadeIn();

        $.ajax({
            method: 'GET',
            url: '/damaso-cod/gallery.php',
            data: "main_dir=client_images&images_dir="+$(this).data('folder'),
            success: function(data){
                $('.modal').html(data);
                $('.modal').cycle({
                    'next': '#next',
                    'prev': '#prev'
                });
            }
        });

        $('#close-btn').on("click", function(e){
            e.preventDefault();
            $('.modal').html('').cycle('reinit');
            $('.filter').fadeOut();
            $('body').removeClass('active');
        });
    });
});
</script>
  • I don’t know if I’ll be able to help, but... Was it supposed to be destroying the cycle anyway? I never messed with Cycle, but according to the documentation, call destroy removes all events Binds.

  • 1

    @Ruipimentel, as I clean all the div’s HTML when I close the lightbox, has no reason to let Cycle work. The intention is to start each click and end each Vex that the lightbox is closed.

  • Retire $('#close-btn').on("click", from within the first $('.featured .client').on("click"

4 answers

1

I got it working on the fiddle: http://jsfiddle.net/jaderw/1f4efku7/

jquery: (I marked below what was modified)

$('.featured .client').on("click", function (e) {
    e.preventDefault();
    $('body').addClass('active');
    $('.filter, .modal').fadeIn(); // MODIFICADO

    $.ajax({
            method: 'GET',
            url: '/damaso-cod/gallery.php',
            data: "main_dir=client_images&images_dir="+$(this).data('folder'),
            success: function(data){
                $('.modal').html(data);
                $('.modal').cycle({
                    'next': '#next',
                    'prev': '#prev'
                });
            }
        });
});

// on click abaixo foi retirado de dentro do on click de cima...

$('#close-btn').on("click", function (e) {
    e.preventDefault();
    $('.filter, .modal').fadeOut(400, function () {
        $('.modal').cycle('destroy').html('');
        $('body').removeClass('active');
    }); // MODIFICADO
});
  • If I’m not mistaken e.preventDefault(); won’t let you repeat an action.

0

Well, by the time Voce closes the lightbox style="display:none" in the div of the said whose, then it will never appear again, the cool would be that every time Voce triggers the button, it adds a style="display:display;" in the class of the div

  • I’m actually using fadein and fadeOut for this...

0

Pedro Vinícius,

Try jQuery 1.7.2 with . live

 <script type="text/javascript">
 $(document).ready(function(){
     $('.featured .client').live("click", function(e){
         e.preventDefault();
         $('body').addClass('active');
         $('.modal').show();

         $.ajax({
            method: 'GET',
            url: '/damaso-cod/gallery.php',
            data: "main_dir=client_images&images_dir="+$(this).data('folder'),
            success: function(data){
                $('.modal').html(data);
                $('.modal').cycle({
                    'next': '#next',
                    'prev': '#prev'
                });
            }
        });
    });

    $('#close-btn').live("click", function(e){
        e.preventDefault();
        $('.modal').html('').cycle('reinit');
        //$('.filter').fadeOut();  nao compreendi
        $('.modal').hide();
        $('body').removeClass('active');
    });
});
  • .live is deprecated and the jquery version indication for an old release does not help. You can use . on with the same functionality as . live

  • The fact of being "deprecated" does not make it bad. I use it constantly without even a kind of problem.

  • 1

    a deprecated command is short-lived, in future releases it will surely be removed, if you continue to use it in new projects, it will only increase the amount of source code that should be refactored when this happens

  • "deprecated" is already an alert for refactoring :)

0

Try using firefox’s Firebug, it probably only works once because it is raising an exception that can probably be in PHP.

  • I’ve debugged in PHP... It’s all right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.