one click, two 'click' event fired

Posted by Toni Michel Caubet on Stack Overflow See other posts from Stack Overflow or by Toni Michel Caubet
Published on 2012-09-01T21:20:52Z Indexed on 2012/09/01 21:38 UTC
Read the original article Hit count: 165

Filed under:
|
|

I just want to toggle some elements when a link is clicked:

This is how i am trying (But i don't really think that it matters much for this question what's inside the event function callback):

/* mostrar exceso de comentarios a peticion del usuario*/
$('.toggleComments').click(function(){
        console.log('.toggleComments');
        if($(this).parents('.helpContent').find('.commentHideble:visible').length > 0){
            $(this).text('+ <?=get_texto_clave('show_old_comments')?>').removeClass('toggleCommentsActive').append(' ('+$(this).parents('.helpContent').find('.commentHideble:not:visible').length+'+)');
        }else{
            $(this).text('- <?=get_texto_clave('hide_old_comments')?>').addClass('toggleCommentsActive');
        }
        $(this).parents('.helpContent').find('.commentHideble').slideToggle(100);

});

I even tried a boolean but gave me same result

/* mostrar exceso de comentarios a peticion del usuario*/


var ctoggle = false;
$('.toggleComments').click(function(){
    if(ctoggle == false){
        ctoggle = true;
        console.log('.toggleComments');
        if($(this).parents('.helpContent').find('.commentHideble:visible').length > 0){
            $(this).text('+ <?=get_texto_clave('show_old_comments')?>').removeClass('toggleCommentsActive').append(' ('+$(this).parents('.helpContent').find('.commentHideble:not:visible').length+'+)');
        }else{
            $(this).text('- <?=get_texto_clave('hide_old_comments')?>').addClass('toggleCommentsActive');
        }
        $(this).parents('.helpContent').find('.commentHideble').slideToggle(100);
        ctoggle = false;
    }
});

Why the log is being fired twice by click?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery