Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if an HTML select element is expanded (without manually tracking state)

Tags:

javascript

I'd like to detect if a drop down is expanded or not. I don't want to use extra event handlers for click/mouseover etc because the drop-downs are dynamic and for other reasons I can't use something like jQuery live. Basically I'd like something that can given an arbitrary select element (no other attached event handlers, classes, etc), can give a true/false answer on whether it is expanded or not.

For my specific application, I am handling mouse wheel events, but don't want to handle them when a drop down is open (which would override the browser default functionality). However, I still want to handle the mouse wheel events when the mouse has hovered over the select, but has not opened it.

like image 389
ScottR Avatar asked May 27 '11 19:05

ScottR


2 Answers

I looked into this before, for similar reasons. I could never find a solution other than trying to track it manually which really doesn't work. There are several ways to open/close a select (drop down) such as Alt+Dn Arrow. An open select will close if the user clicks on something outside the browser. Trying to keep track of the state of the select is an exercise in futility. Unless someone else comes along with something I missed on my hunt, you'll have to code around it as elegantly as you can.

like image 141
ic3b3rg Avatar answered Oct 26 '22 22:10

ic3b3rg


How about when it's got focus, even if it isn't expanded? You specifically ask for expanded because you don't want to override default browser behaviour, but the browser behaviour should be to scroll through the items when the item is focussed, even if it isn't expanded, so I would say you'd be better off detecting focus.

If you're okay with that, then you can certainly easily detect when a field has focus and when it loses it, by using the JQuery focus() and blur() methods, or focusin() and focusout().

http://api.jquery.com/focus/ and http://api.jquery.com/blur/

http://api.jquery.com/focusin/ and http://api.jquery.com/focusout/

Hope that helps.

like image 42
Spudley Avatar answered Oct 26 '22 22:10

Spudley