I have a menu that opens hidden divs using on click function. Id like to make them close when clicking outside of the toggled divs. I've tried few things but it renders a click function so it doesn't close hidden div when clicked again.
$(document).on('click', '.user-alt > li > a', function(e){
var popup = $(this).parent('li').find('.bar-pop');
$(".user-alt > li .bar-pop").not(popup).hide();
$('.menu-accountparent.selectedMenuItem').not($(this)).removeClass('selectedMenuItem');
$(this).toggleClass('selectedMenuItem');
popup.toggle();
});
Here is JSfiddle: http://jsfiddle.net/fkrosq8w/
I've tried these and works but like I said clicking on link in menu again does not close hidden box:
$('body').on('click', '*:not( .user-alt > li .bar-pop )', function() {
var popup = $('.user-alt > li .bar-pop');
popup.hide();
});