How to close the mobile menu by clicking elsewhere

So I have a mobile menu in my mobile website but I’m trying to make it so when you click anywhere but the buttons in the drop down menu, it closes the mobile menu.

But I have very limited experience with JS and I have found something close to what I’m looking for, [SOLUTION] Close panel when clicking outside the box.

But the problem with that solution is that I’m having trouble adapting it to the mobile menu. Any ideas how to do it?