//const body = document.body; function findAncestor(el, cls){ while ((el = el.parentElement) && !el.classList.contains(cls)); return el; } // Event delegation. Body click, then check for buttons body.addEventListener('click', (e) => { if(e.target.classList.contains('button-close')){ let alertD = findAncestor(e.target, 'alert--dismissable'); if (typeof(alertD) != 'undefined' && alertD != null){ alertD.remove(); } } });