calendar.onclick = function(event) { var target = event.target; while (target != this) { if (target.classList.contains('calendar-day')) { highlight(target); target.appendChild(div); break; } target = target.parentNode; } };