Simply function to detect click event outside desired element:
document.onclick = function (e) {
e = e || event
var target = e.target || e.srcElement
var element = document.getElementById('id_element');
do {
if (element == target) {
// When clicked inside element, does nothing
return;
}
target = target.parentNode;
} while (target)
// When clicked outside element, makes an action.
element.style.display = 'none';
}