javascript - How do i open a <select> menu in by pressing enter on keyboard - Stack Overflow

For amenu in html, clicking on the select will show the drop down with options, but how would I trigg

For a menu in html, clicking on the select will show the drop down with options, but how would I trigger this by clicking on the enter key. I have tried setting up a keyup listener which would then trigger the 'click' event, but the menu is not showing up

For a menu in html, clicking on the select will show the drop down with options, but how would I trigger this by clicking on the enter key. I have tried setting up a keyup listener which would then trigger the 'click' event, but the menu is not showing up

Share Improve this question asked May 19, 2019 at 23:24 Tenzin ChoklangTenzin Choklang 5232 gold badges5 silver badges24 bronze badges 1
  • This is not currently possible unfortunately: stackoverflow./questions/6992639/… – aprouja1 Commented May 19, 2019 at 23:44
Add a ment  | 

2 Answers 2

Reset to default 1

Why not try an easier approach? Select opens when you hit enter when it has focus on it, so basically you need only to autofocus when the page load. Example:

<select id="dropdown" autofocus class="" name="">
 <option value="">Opt1</option>
 <option value="">Opt2</option>
 <option value="">Opt3</option>
</select>

If you still want to trigger the event EVERY time enter is hitted, you can do this:

window.addEventListener('keypress', function (e) {
 if (e.keyCode == 13) {
  document.getElementById("dropdown").focus();
 }
}, false);

Basically, select gain focus when you hit enter, then you can hit again to open it.

To answer my own question, the options are opened by using either the up/down arrow keys once the field is in focus. Its something that is build in the html, so no need to create separate listeners for it

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655468a4638519.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信