I have a <div> that exists on a page and I need to make it so that when the user clicks outside of that element it will become hidden, but if the user clicks somewhere within the element, then it should stay.
I tried using
e.stopPropagation();
and
e.preventDefault();
adding it to the click event of that certain DIV but that didn’t work.
Thanks!
"click") todocumentorwindowusing EventTarget.addEventListener()!– in order to check whether the Event.target (the element that initiated the Event) – its self or closest ancestor have a specific selector.Usage example: Close popup modal on click outside