Check If Select Is Displaying Options April 06, 2023 Post a Comment I have the following HTML code: How do I check if the s of the are displayed? For example, this is considered as the s of the Solution 1: 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()); } } });Copy <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>Copy Solution 2: You can use change event in case of not displayed, and focus event for displayed $("#myselect").on({ "change": function() { $(this).blur(); }, 'focus': function() { console.log("displayed"); }, "blur": function() { console.log("not displayed"); }, "keyup": function(e) { if (e.keyCode == 27) console.log("displayed"); } });Copy <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>Copy Solution 3: After reading, I´ve realized that Option tags are not "usual" DOM elements. Then, is not so easy to control it (instead you are checking click, blur, and combining other methods as we started trying before). Now I´ve managed you finally found the solution in a mix of the first idea we had, then, i´ll just explain the other possible solutions you have and all the things I´ve learned about selects: If you DON´t add a size attribute to the select, you will not be able to detect keypress at the time it´s open. If you add a size attribute bigger than 1 to the select, it will detect keypress, but will loose it´s format and a list will appear. If you try to use offset, and similar, to check wether the select is open, is also not working, as the option objects do not have offset functions... (they are not usuarl DOM objects) Click/focus will also not be enough, as the focus remains in select after a "ESC" click. Then, after all those things reviewed, I think it´s not possible to make it in a pure HTML way without doing a lot of click/blur controls like answered before. Anyway, to clarify, there are lots of jquery librarys that will help you to simulate a select/option dropdown, and to control if it´s open. I hope this answer will solve your Question. Check this functional fiddle I´ve prepared for you to show the jquery dropdown way: $('#jq-dropdown-1').on('show', function(event, dropdownData) { console.log("SHOWN"); }).on('hide', function(event, dropdownData) { console.log("HIDDEN"); });Copy <link href="https://rawgit.com/claviska/jquery-dropdown/master/jquery.dropdown.min.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://rawgit.com/claviska/jquery-dropdown/master/jquery.dropdown.min.js"></script> <a href="#" data-jq-dropdown="#jq-dropdown-1">dropdown</a> <div id="jq-dropdown-1" class="jq-dropdown jq-dropdown-tip"> <ul class="jq-dropdown-menu"> <li><a href="#1">Item 1</a> </li> <li><a href="#2">Item 2</a> </li> <li><a href="#3">Item 3</a> </li> <li class="jq-dropdown-divider"></li> <li><a href="#4">Item 4</a> </li> <li><a href="#5">Item 5</a> </li> <li><a href="#5">Item 6</a> </li> </ul> </div> <button id="clickme">click me</button>Copy Those are the docs I´ve read to understand it: jQuery keypress/keyup/etc events not fired when select box is expanded How to detect escape key press with JavaScript or jQuery? jQuery Event Keypress: Enter key Keypress event on chosen single select Solution 4: Im suggesting you to use new jquery selectmenu which is providing selectmenu open event $("#myselect").selectmenu({ open: function(event, ui) { alert("opened"); } });Copy label { display: block; } select { width: 200px; }Copy <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <select id="myselect"> <option selected>Test 1</option> <option>Test 2</option> <option>Test 3</option> </select>Copy Ref : http://api.jqueryui.com/selectmenu/#event-open Solution 5: What about simple check when jquery is 1.6+ if ($(".selector").is(":focus")){ // do stuff } Copy or $(".selector").on('focus', function() { // do stuff }); Copy It can do the trick. Share Post a Comment for "Check If Select Is Displaying Options" Top Question WebRTC Video Constraints Not Working I'm trying to get a lower resolution from the webcam na… Remove Content From Table And Reformat The Table I want to do My page is displaying data of the table in be… HTML Table With Fixed Header And Fixed Leading Columns With Knockout Controls In The Header And First Column I have a HTML table which can be customized by the user: E… How Do I Make A Menu Pop Up When The User Clicks The Menu Button? I'm new to web development and I dont have much experie… Position This Div In The Center Of It's Container? Before you attempt to solve this please carefully read the … ASP.NET MVC 3 - DropDownListFor Fails When URL Parameter Also Exists With Same Name As View Model Parameter I am not sure if this is a bug with the DropDownListFor ext… What Is The Correct Way For Redirecting From The Domain Name Itself To A Page In A Directory? I want anyone browsing for 'MyDomain.com' to see wh… Disable Browser Caching Html5 I would like to know how to disable the browser cache, usin… How Can I Display Bold Text In A Textarea? I have this code: test It displays the raw HTML code. Ins… Bs4 Breaks Html Trying To Repair It BS4 corrects faulty html. Usually this is not a problem. I … December 2024 (1) November 2024 (37) October 2024 (62) September 2024 (21) August 2024 (361) July 2024 (333) June 2024 (698) May 2024 (1287) April 2024 (812) March 2024 (1552) February 2024 (1709) January 2024 (1382) December 2023 (1399) November 2023 (447) October 2023 (585) September 2023 (294) August 2023 (334) July 2023 (265) June 2023 (330) May 2023 (206) April 2023 (150) March 2023 (139) February 2023 (168) January 2023 (259) December 2022 (130) November 2022 (229) October 2022 (178) September 2022 (159) August 2022 (289) July 2022 (105) Menu Halaman Statis Beranda © 2022 - Html5 Coding