toggle outside of click should close menu code example
Example: Click Outside Close Menu Box
window.addEventListener('mouseup', function(event){
var box = document.getElementById('box1');
if (event.target != box && event.target.parentNode != box){
box.style.display = 'none';
}
});