Need help with a simple Toggle script

I found a simple toggle script which opens and closes a div upon clicking the link. Is there a way for it to toggle without click the designated link… or in other words, click anywhere on the page.

function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == ‘block’)
e.style.display = ‘none’;
else
e.style.display = ‘block’;

}

onclick=“toggle_visibility(‘idname’);”

Thank you!

I’m not sure on the ‘usability’ of such a thing, but if you’re sure, just add the onclick event to the body tag rather than the link.

Basically I am using the function to toggle a sub-menu. So I made a menu. The issue is that Users don’t want to click the link to close the menu. They just want to click anywhere to close the menu,.

With that being said, I have several menus all using this single function (using different ID names) - would there be a way to put a single onclick in the body that would affect all?

Im not a javascript guy, so copying and pasting is my thing not editing code :wink: