I've been searching for solutions to address this issue, but so far nothing has worked. Here is the JavaScript code I am using:
var specifiedElement = document.getElementById('a');
document.addEventListener('click', function(event) {
var isClickInside = specifiedElement.contains(event.target);
if (!isClickInside) {
alert('You clicked outside A and B')
}
});
div {
background: #aaa;
height:2em;
padding: 1em;
margin-bottom:10px;
text-align: center;
}
<div id="a">A</div>
<div id="b">B</div>
(View in JS Fiddle: https://jsfiddle.net/1zj9dmq7/)
I am trying to achieve a functionality where the "alert" function only runs when clicking outside of elements a and b, not when clicking on them directly, and without using jQuery. If anyone can assist me with this, I would greatly appreciate it. Thank you.