I have used this JavaScript below:
This is not the most efficient way, but it will work. The idea is to traverse the tree and check if the parent is the id of the one where you don't want to hide on click anywhere except it.
$(document).on('click', function(e) {
var p = e.target;
while(p) {
console.log(p);
if(p.id) {
if(p.id == 'openModal') {
return;
}
}
p = p.parentElement;
}
$("#openModal").hide();
});