Search code examples
jquerypopupremoveclass

jQuery removeClass not working on popup


Here is my code

jQuery('.woshadow').click(function(){
    jQuery(this).find('.popup-list').addClass('popup-listh');
    jQuery(this).find('.close-popup').addClass('popup-listh');
});

jQuery('.close-popup').click(function(){
    jQuery(this).parent().find('.popup-list').removeClass('popup-listh');
    jQuery(this).removeClass('popup-listh');
});

when i use addClass instead of removeClass it's fine and work properly but it is not working with removeClass that is realy rare any suggestion please?


Solution

  • If you click on .close-popup, event propagate to .woshadow level, and so re-add class

    Simple fix is to stop event propagation on child element:

    jQuery('.close-popup').click(function(e){
        e.stopPropagation();
        jQuery(this).parent().find('.popup-list').removeClass('popup-listh');
        jQuery(this).removeClass('popup-listh');
    });