I am using this code to control the display of a sidebar via add/remove class.
The first 2 functions work, however I have also added a 3rd function to try to remove class active when clicking outside of the sidebar.
Now nothing works, it just pushes some body content aside.
jQuery(document).ready(function($){
$(".btn-open-sidecart").click(function(){
$('.sidebar').addClass("active");
$('.overlay').addClass("active");
});
$(".btn-close-sidecart").click(function(){
$('.sidebar').removeClass("active");
$('.overlay').removeClass("active");
});
$('body').click(function(event){
if($(event.target).attr('class') !== "sidebar" && $(event.target).attr('class') !== "btn-close-sidecart") {
$('.sidebar').removeClass('active');
$('.overlay').removeClass('active');
};
});
});
How can I fix this to also removeClass('active')
when clicking outside the sidebar?