1
|
I'm trying to create a simple cross browser plugin for buttons with dropdown menus. When user would click such button a menu would appear underneath with various options and user can subsequently select an option from it or close it.
I've created a simple JSFiddle with three such buttons that exemplify what I'm trying to achieve. My JSFiddle code does some additional event logging that I've excluded from below code, but running JSFiddle makes it pretty obvious that I'm logging events as they happen.
This is my HTML:
The way that I implemented my code I need dropdown menu focusable, hence the
tabindex attribute on the container.
My script:
Displaying of the menu is done by CSS. As you can see I barely set a CSS class on the container and CSS provides automatic visibility when
open class is set on container.Intended behaviour
This is the correct way as it should work:
Browser issues
Different browsers seem to fire events differently and excessively. Event propagation (bubbling) and their sequence prevents upper steps to execute as expected. Chrome seems to not fire excessive events.
Chrome
Chrome seems to work as expected. All four steps execute as they should. When clicking on a link within the menu, no focusout is being fired as the link in within focused container (the menu itself).
Firefox and IE9
It seems that steps #1, #2 and #4 work as expected, but #3 fails because before menu option click can be detected and executed, focusout fires first and closes the menu.
IE8 and IE7
Anybody that has them can test for me and tell me which of the upper steps work and which fail. I haven't tested but would really like to know as well. Question
The main issue with this script is that focusout event fires prematurely and too often. I can't use blurevent because it's not propagated from menu options to menu itself.
Anybody wants to play with these events in a cross browser way?
| |||
0
|
Can use a click handler on document to replace the
focusout code. Not exactly sure of behavior you want but try this:
This could be refined to only add document click handler when a menu is open and remove it when all menus are closed
| ||
show 2 more comments |
0
| Cross browser solution
Solution I've come up with is cross browser and works in Chrome, Firefox and IE7+. It required an additional event to be handled and that is the
mousedown of the dropdown menu. Clicking an option on a dropdown menu normally fires a focusout event in IE and FF, even though user clicked within the same element that is in focus. That's why we set next focusout to be ignored and not close the menu.
Chrome does not fire
focusout menu option clicks, so we also have to handle that by manually re-enabling closing after some short enough time. I've set it to 100ms, but it can be much shorter as it only needs to be delayed until next focusout handler is being executed. It seems that 10ms is also enough. Maybe even less if event handlers are all being queued by the browser before they start executing. In that case a value of 0 would be sufficient. But to make it safe I've left it on 100ms.
This is the code that does what's expected:
|
target
as I did here – charlietfl Jan 13 at 1:55focusout
on the other hand works as expected. Whenever user clicks anywhere (even within developer tools), element would loose focus and menu would hide. and BTW your code has a bug because simply clicking within the frame doesn't really close the menu. Menu only gets closed when a different drop down button gets clicked. – Robert Koritnik Jan 13 at 1:59