page event to hide floating div

J

Jeremy

I've got a floating div which becomes visible when a link is clicked. I
want the div to be hidden when the user clicks anywhere on the page except
for whithin the div. What is the best way to do this? Really, I only need
to support ie6+ but cross browser is always nice.
 
D

Dan Rumney

I've got a floating div which becomes visible when a link is clicked. I
want the div to be hidden when the user clicks anywhere on the page except
for whithin the div. What is the best way to do this? Really, I only need
to support ie6+ but cross browser is always nice.

You could use the BODY element's onClick event handler.

However, this is called whenever you click anything on the page, this
handler will get called.

The big questions are

1) Which will get called first, the floating div or the body?
2) How do you prevent the body's handler being called when you click
on the floating div?

http://www.quirksmode.org/js/events_order.html seems to be a pretty
respectable writeup of this very issue
 
J

Jeremy

That's a good idea, the other issue that could happen is that the body
could already have an onclick event, in which case I wouldn't want to
overwrite it, I'd have to write code to handle chaining the events, and
restoring the original event after. As well, because I'm doing this as a
resusable component, what if the onclick event gets overwritten by someone
else.

I stumbled uppon the attachEvent and detachEvent
(addEventListener/removeEventListener for mozilla) methods. They allow you
to keep adding events to an element.
 
L

Lee

This should work with IE6+ and Firefox. I havent tested on any other
browsers.

<html>
<body onclick="HideDiv(event)">
<a href="Javascript: ShowDiv()">Show Div</a>
<div id="floating" name="floating" style="border:solid 2px red;width:
200;height:200;display:none;" >WELCOME
<div><input id="test" name="test" value="test"></div>
</div>
<Script>
var myFloatingDiv = "floating"

function ShowDiv() {
document.getElementById("floating").style.display = "block";
}
function HideDiv(e) {
if(!e) e = event;
var myElement = e.target || e.srcElement;
var displyProperty = "none"
for(;myElement != null ;myElement = myElement.parentNode) {
if(myElement.id == myFloatingDiv) {
displyProperty = "block";
break;
}
}
document.getElementById(myFloatingDiv).style.display =
displyProperty;
}
</script>
</body>
</html>
 
L

Lee

That won't do what the OP is looking for in the case that the user
clicks outside of the floating div, but not directly on the page
background... eg on an image or a button.

Whether this is a significant problem depends on the makeup of the rest
of the page.

That alternative is to prevent the event from bubbling up from the
floating div. That means that the floating div will catch all onClick
events associated with it and its children and prevent them from
reaching the body's onclick handler.- Hide quoted text -

- Show quoted text -

The request was to hide the div if anything on the page (Image,
button, link, etc.) was clicked other than within the div in
question.

Of course there are instances where this would break. For example: if
a image, button or other element on the page (not in the hidden div)
had script that canceled the bubble.

But, Images and buttons should bubble up to the body onclick event and
fire the HideDiv() function (given that they do not
window.event.cancelBubble = true; as I mentioned earlier). The
HideDiv() function is smart enough to know if the event was fired from
within the div or not.

Of course you could do onclick="window.event.cancelBubble = true;"
inside the hide able div, but you would still need a function that
hides the div on the body. Both my last post and the following code do
the same thing, just in different ways.

Example of cancelBubble in div:
--------------------------------------------------------------------------------------
<html>
<body onclick="HideDiv(event)">
<a href="Javascript: ShowDiv()">Show Div</a>
<div id="floating"
name="floating"
style="border:solid 2px red;width: 200;height:200;display:none;"
onclick="window.event.cancelBubble = true;">
WELCOME
<div>
<input id="test" name="test" value="test" onclick="alert('u did
it')">
</div>
</div>
<BR><BR>
<img src="pagerror.gif" onclick="alert('hi')">
<input type=button value=test onclick="window.event.cancelBubble =
true; alert('dont press me')">
<Script>
var myFloatingDiv = "floating"

function ShowDiv() {
document.getElementById(myFloatingDiv).style.display =
"block";
}

function HideDiv(e) {
document.getElementById(myFloatingDiv).style.display = "none"
}
</script>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top