Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a <select> dropdown menu is open?

Tags:

I'm looking for a way to determine if/when a <select> element's menu is open. I don't need to force it to open or close, just figure out if it's open or closed at a given time.

I can listen to events for focus/blur, mouseup/mousedown, etc., but I don't think I can reliably figure out the state of the menu from those events. For example, mousedown followed by mouseup could mean the user clicked and dragged to a selection and released (in which case the menu is now closed) or clicked and released to open the menu (in which case the menu is open). It also seems likely that the specific behavior of dropdown menus is browser-dependent.

I know I could do this if I roll my own dropdown menu, but I prefer to use <select>.

Is there a reliable way to find out if a dropdown menu is open? Or is this something that Javascript can't know?

Conclusion: There doesn't seem to be any guaranteed way to determine if a select menu is open, either by asking the object or by listening to the events it fires.

For my own use I'm just keeping track of whether the select has focus using onfocus and onblur. I'm assuming there's no way for the menu to be open without having focus, and that seems to hold true in all the browsers I've tested. It doesn't actually tell me when the menu is open, but it tells me when it can't be open, which is good enough for my purposes.

like image 408
Robert Avatar asked Apr 25 '10 18:04

Robert


People also ask

How do I know which option is selected in dropdown?

You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.

What is the difference between dropdown and select?

Select versus DropdownUse the select component inside a form where users are selecting from a list of options and submitting data. Use the dropdown component to filter or sort content on a page.

What does select from dropdown mean?

When activated, it displays (drops down) a list of values, from which the user may select one. When the user selects a new value, the control reverts to its inactive state, displaying the selected value. It is often used in the design of graphical user interfaces, including web design.


2 Answers

You can play with mouseenter event in select's option child elements as you can only enter in them if select menu is open, or also with the click event on the select element, usually thrown when you open it.

To test in a precise moment if its open or not I think is not possible.

like image 190
maid450 Avatar answered Oct 03 '22 20:10

maid450


Select elements are notoriously tricky in Internet Explorer. I don't think there is any way of reliably determining if one is open or closed.

like image 29
Jakob Kruse Avatar answered Oct 03 '22 20:10

Jakob Kruse