I\'m looking for a way to detect if a click event happened outside of a component, as described in this article. jQuery closest() is used to see if the target from a click e
Alternatively to .contains, you can use the .closest method. When you want to check if a click was outside of the element with id="apple" then i can use:
const isOutside = !e.target.closest("#apple");
This checks if any element in the tree above the clicked one has an id of "apple". We have to negate the result!