Hide box when click outside

Depending on where it is on the page you will have to put an onClick event handler function on the page background itself or something behind the box like a larger transparent box that the user can click on and make the box element hide.

Think of it as as a lightbox, where you can close the lightbox with the ‘x’ button or the close button, or you can setup the overlay to be clickable and set to close the lightbox if it is clicked on.
https://support.wix.com/en/article/allowing-site-visitors-to-close-a-lightbox-by-clicking-the-overlay

Otherwise you can simply change the event handler used and maybe use the onMouseIn and onMouseOut functions instead.