function isChild(child, parent) {
var current=child;
if(!child||!parent)return false;
if(child===parent)return true;
while (current = current.parentNode) {
if(current==parent)return true;
}
return false;
}
...
if(!isChild(event.target, popupElement)){
popup.close();
}