![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
|
On Jun 3, 11:41 am, "Jeremy" <nos... (AT) please (DOT) com> wrote: 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 |
#4
| |||
| |||
|
|
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. |
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |