$(".btn-cart").click(function () { $(".cart").toggleClass("cart-active"); }); 

You need to perform this function if the block (".cart") has a class ("cart-active")

 $(document).click(function (event) { if ($(event.target).closest(".cart").length){ return; } else { $(".cart").toggleClass("cart-active"); event.stopPropagation(); } }); 

    1 answer 1

    It's simple)

     if ($(".cart").hasClass("cart-active")) { event.stopPropagation(); } 
    • The hasClass () method does not work because the class ("cart-active") adds jQuery. and hasClass () will check on the html document itself whether there is a class. - Dreamer
    • have you checked? since $ (". cart"). hasClass ("cart-active") is also jQuery, and it is tied only to the .cart block - GENESIS