I want to attach event to overlay so tht i can close the overlay on any click in the page, but not if you click on the overlay. (dont close if youclick on the overlay)
I am usingdocument.body.attachEvent("onclick", closeoverlay), but its not working, this will close the overlay even if you click on the overlay. I want to close the overlay only if there is a click event on anywhere the page except on the overlay.
Here is the code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
The key is to query the trigger element that caused the event. You should also know that attachEvent() and detachEvent() are IE-only.
Code:
<script type="text/javascript">
function closeoverlay(e) {
var trigger = e.target ? e.target : e.srcElement; //only run if trigger is not overlay
if (trigger.id != 'overlay') document.getElementById('overlay').style.display = 'none';
}
if (window.addEventListener)
document.addEventListener('click', closeoverlay, false);
else if (window.attachEvent)
document.attachEvent('onclick', closeoverlay);
</script>
<body>
<div style="width: 300px; height: 200px; position: absolute; left: 300px; height: 200px; background: #f90;" id="overlay">
I am the overlay element
</div>
</body>
basically i have to show overlay when user click on a link (say showOverlay) and i want to attach the event only
when this overlay is shown. so i attached event in the showoverlay function.
document.attachEvent('onclick' closeoverlay); //lets assume for IE case
when i click on showOverlay link, the overlay appears and disappears immidiately.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<style type="text/css">
#overlay {
position: absolute;
display:none;
}
</style>
<script type="text/javascript">
function showOverlay(){
//alert("inside");
document.getElementById('overlay').style.display="block";
//document.onclick=closeoverlay;
document.attachEvent('onclick', closeoverlay);
}
function closeoverlay(e){
var target=e?e.target:event.srcElement; // Moz&W3C : IE
var mydiv=document.getElementById('overlay');
target!=mydiv?mydiv.style.display='none':null;
}
</script>
</head>
<body>
<a href="#" onclick="showOverlay();">Show Overlay </a>
<div style="width: 300px; height: 200px; position: absolute; left: 300px; height: 200px; background: #f90;" id="overlay">
I am the overlay element
</div>
</body>
</html>
Last edited by Kor; 01-04-2010 at 12:34 PM.
Reason: wrap the code [code][/code]
This just displays the overlay and when clicks on the link again it disappears, i want to close the overlay when user clicks anywhere on the page except on the actual overlay.
Thanks, it works i tried event adding document.onclick=closeoverlay; inside the showOverlay() method, so that the event is attached only when the overlay is opened.
Bookmarks