This is actually possible with HTML + Javascript, despite the fact that everywhere people say that it is not.
However, this only works on Chrome. Read more if you are interested.
According to the W3C Working Draft for HTML5, section 3.2.5.1.7. Interactive Content :
Some elements in HTML have activation behavior, which means that the user can activate them. This triggers a sequence of events depending on the activation mechanism [...] , for example, using the keyboard or voice input or mouse clicks .
When a user launches an element with a specific activation behavior in a way other than clicking on it, the default action for the interaction event should consist in performing activation steps for artificially clicking on the element.
<select> , being interactive content, I thought it was possible to programmatically display its <option> . After hours of playing, I found that using document.createEvent() and .dispatchEvent() worked.
However, the demo time. Here is a working fiddle.
HTML:
<select id="dropdown"> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> </select> <br> <button id="fire" type="button" onclick="runThis()">Show dropdown items</button>
Javascript:
// <select> element displays its options on mousedown, not click. showDropdown = function (element) { var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; // This isn't magic. window.runThis = function () { var dropdown = document.getElementById('dropdown'); showDropdown(dropdown); };
If someone finds a way to do the same, but not in Chrome, feel free to change this fiddle .
Xavier Ho Apr 13 2018-12-12T00: 00Z
source share