Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if select is displaying options

Tags:

I have the following HTML code:

<select>  <option selected>Test 1</option>  <option>Test 2</option>  <option>Test 3</option>  </select>

How do I check if the <option>s of the <select> are displayed? For example, this is considered as the <option>s of the <select> are displayed:

Options of the select menu are displayed

And this is considered that the <option>s of the <select> are not displayed:

Options of the select menu are not displayed


I have tried this:

$("#myselect").on("click", function() {      if ($("#myselect option").length == 0) {          console.log("not displayed");      } else {          console.log("displayed");      }   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <select id="myselect">  <option selected>Test 1</option>  <option>Test 2</option>  <option>Test 3</option>  </select>

But the console logs "displayed" all the time.


So how can I achieve this?


EDIT 1:

The answers at How to check if an select element is still “open” / active with jquery does not work because when I click the select element to display the options then click it again, the options are not displayed even though the select is still focused.


EDIT 2:

Just in case I wasn't explicit enough, basically I want the console to log "displayed" or "not displayed" the user clicks on the select or the options

like image 621
chris97ong Avatar asked Jun 09 '15 10:06

chris97ong


People also ask

How do you check if the Select option is selected or not?

$('#mySelectBox option'). each(function() { if ($(this). isChecked()) alert('this option is selected'); else alert('this is not'); });

How can I check whether a option already exist in select by JavaScript?

Show activity on this post. var exists = $("#yourSelect option") . filter(function (i, o) { return o. value === yourValue; }) .

How do I get select option in HTML?

HTML <select> Tag.

What is option selected?

The selected attribute is a boolean attribute. When present, it specifies that an option should be pre-selected when the page loads. The pre-selected option will be displayed first in the drop-down list. Tip: The selected attribute can also be set after the page loads, with a JavaScript.


1 Answers

You can try listening on click, blur and key press event. I am just toggling a open variable to true or false on each of the event.

   // if menu is open then true, if closed then false     // we start with false     var open = false;     // just a function to print out message     function isOpen(){         if(open)            return "menu is open";         else            return "menu is closed";     }     // on each click toggle the "open" variable     $("#myselect").on("click", function() {           open = !open;           console.log(isOpen());     });     // on each blur toggle the "open" variable     // fire only if menu is already in "open" state     $("#myselect").on("blur", function() {           if(open){              open = !open;              console.log(isOpen());           }     });     // on ESC key toggle the "open" variable only if menu is in "open" state     $(document).keyup(function(e) {         if (e.keyCode == 27) {            if(open){              open = !open;              console.log(isOpen());           }         }     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <select id="myselect">  <option selected>Test 1</option>  <option>Test 2</option>  <option>Test 3</option>  </select>
like image 83
Sami Avatar answered Oct 11 '22 16:10

Sami