Forum Moderators: open

Message Too Old, No Replies

Click anywhere to hide Div's Popup

         

slavecode

12:43 am on Aug 20, 2009 (gmt 0)

10+ Year Member



I have a small script to display and close/hide the popup. The problem is I couldn't figure out a way to hide/close the popup by clicking anywhere on a page. I have to click on the link to close it. Is there anyway I can do to close the popup by clicking anywhere on a page? Thanks.

Below is the entire script:

<html>
<head>
<script type="text/javascript">
function ShowPopUp( DivPopUpID, DivMainID )
{
var DivMain = document.getElementById(DivMainID);
if ( DivMain != null )
{
DivMain.disabled = true;
}

var DivPopUp = document.getElementById(DivPopUpID);
if ( DivPopUp != null )
{
DivPopUp.style.display = "block";
}
}
function HidePopUp( DivPopUpID, DivMainID )
{
var DivMain = document.getElementById(DivMainID);
if ( DivMain != null )
{
DivMain.style.display = "block";
DivMain.disabled = false;
}
var DivPopUp = document.getElementById(DivPopUpID);
if ( DivPopUp != null )
{
DivPopUp.style.display = "none";
}
}
</script>

</head>
<body>

<div id="DivMain">
<a href="#" onclick="ShowPopUp('DivLogin', 'DivMain'); return false;">Click Show</a>
<br />
<a href="#" onclick="HidePopUp('DivLogin', 'DivMain'); return false;">Click Hide</a>

</div>

<div id="DivLogin" style="border-right: blue 4px double; padding-right: 1px; border-top: blue 4px double; display: none; padding-left: 1px; font-size: 10pt; left: 25%; visibility: visible; padding-bottom: 1px; margin: 1px; vertical-align: baseline; border-left: blue 4px double; width: 300; padding-top: 1px; border-bottom: blue 4px double; font-family: Tahoma; position: absolute; top: 30%; height: 100px; background-color: #ffffff; text-align: center">

<div style="text-align: left" align="center">
Content inside the popup goes here...
</div>
</div>

</body>
</html>

eelixduppy

4:35 am on Aug 20, 2009 (gmt 0)



You should be able to add a click event to the body...

document.body.onclick = function() {
......
}

See if that doesn't help.

You might also want to have one function called, perhaps, changePopup that does both the hiding and the showing by checking to see which status it is currently in and does the opposite. That way you can just add that right into the body onclick function.

... and Welcome to WebmasterWorld!

slavecode

5:53 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



thanks.

still didn't work.

I added:

<body onclick="document.body.onclick(HidePopUp(DivPopUpID, DivMainID));">

but to no avail.

whoisgregg

7:12 pm on Aug 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By defining the onclick event in the onclick attribute, your onclick isn't defined until the second click... Although, since that method may also override the original onclick, each time you click you're actually redefining the onclick and never actually activating it.

Pretty confusing, huh? :)

If you want to define it directly in the body tag, try this instead:

<body onclick="HidePopUp(DivPopUpID, DivMainID);">