Monkuar Posted August 19, 2012 Share Posted August 19, 2012 I am trying to hide the menu if a user has clicked outside of the div (Menu) function pe(a){return document.getElementById(a)} function CngClass(obj) { obj.className = 'userinfo radius2 userinfodrop'; // Shows the menu/etc pe('show').id = 'show2'; //Shows menu var open = true; } document.onclick=function(){ if (open == true){ //Close the div or try to... pe('show2').style.display='none'; } } Okay, so pretty much when they onclick="CngClass" it will make the element id ('show') to show2, which will show the menu. And set a var open to true right? Now if they click anywhere on the document, I want to check if open == true, to hide the div.................... It's pretty stupid how I set the var open = true and it still doesn't even recognize it after, pretty pathetic if you ask me. Quote Link to comment https://forums.phpfreaks.com/topic/267299-trying-to-hide-element-if-clicked-out-of-the-div-so-simple-but-so-hard/ Share on other sites More sharing options...
Monkuar Posted August 19, 2012 Author Share Posted August 19, 2012 I dont want to waste anyones time, but I found a solution.. function hide(id) { elm = document.getElementById(id) elm.style.display == "none" ? elm.style.display = "block" : elm.style.display = "none" } document.onmousedown = function() { hide('show2') } topic solved.. sorry for me being a comlpete jew edit: wait, no unsolved because they can just click and it will toggle it.. sht............. Quote Link to comment https://forums.phpfreaks.com/topic/267299-trying-to-hide-element-if-clicked-out-of-the-div-so-simple-but-so-hard/#findComment-1370620 Share on other sites More sharing options...
codefossa Posted August 23, 2012 Share Posted August 23, 2012 function hide(id) { window.document.getElementById(id).style.display = "none"; } Quote Link to comment https://forums.phpfreaks.com/topic/267299-trying-to-hide-element-if-clicked-out-of-the-div-so-simple-but-so-hard/#findComment-1371656 Share on other sites More sharing options...
Adam Posted August 23, 2012 Share Posted August 23, 2012 You can do this with a little DOM event trickery. Knowing that events bubble up through the DOM tree, the idea is to bind a click event to the document that will always hide it, but then another event on the menu wrapper that will intercept events bubbling up and stop the propagation. That way any click events originating from within the menu, will travel up the DOM tree until they hit the wrapper's click handler and then stop. Any click events originating from outside of the menu will travel all the way up to the document and so hide the menu. Just be sure to only bind this behaviour when the menu is showing, and then unbind it when it's hidden. Quote Link to comment https://forums.phpfreaks.com/topic/267299-trying-to-hide-element-if-clicked-out-of-the-div-so-simple-but-so-hard/#findComment-1371793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.