Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an select element is still "open" / active with jquery

Tags:

jquery

forms

Is there anyway I can detect via jQuery if a select form element is active at the moment?

I created this fiddle to demonstrate the problem:

http://jsfiddle.net/E2PhT/2/

When you hover over the "Search"-Link the search form shows up. When you now click on the select-field to open the options inside and then, without choosing anything, leave the form on the right side (move the mouse past the right edge of the grey block), the select-field stays open while everything eles disappears (as it should).

Now I would like to either make the select element inactive again, so it doesn't stay open or prevent the rest of the form from disappearing as long as the select element is still open.

So either way I have to somehow detect if the select field is still open or active. Is there any way to check this state with jQuery?

Thx for help.

like image 584
TimG Avatar asked Jun 20 '12 16:06

TimG


People also ask

How check Div is active or not in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How to select a element using jQuery?

jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors. All selectors in jQuery start with the dollar sign and parentheses: $().


1 Answers

You can try :focus selector:

if (!$("select").is(":focus")) {
    // ...
}

DEMO: http://jsfiddle.net/E2PhT/3/

like image 168
VisioN Avatar answered Nov 14 '22 23:11

VisioN