Skip to content

Commit 6177c72

Browse files
committed
Make dropdown work with keyboard navigation
1 parent 3d432d2 commit 6177c72

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/javascript/vanilla-js-dropdown.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ var CustomSelect = function(options) {
6363
selectContainer.appendChild(ul);
6464

6565
selectContainer.addEventListener('click', onClick);
66+
selectContainer.addEventListener('keypress', function(event) {
67+
if (event.key === 'Enter') {
68+
onClick(event);
69+
}
70+
});
6671

6772
// pseudo-select is ready - append it and hide the original
6873
elem.parentNode.insertBefore(selectContainer, elem);
@@ -80,6 +85,7 @@ var CustomSelect = function(options) {
8085
li.innerText = options[i].textContent;
8186
li.setAttribute('data-value', options[i].value);
8287
li.setAttribute('data-index', index++);
88+
li.setAttribute('tabindex', '0');
8389

8490
if (selectOptions[elem.selectedIndex].textContent === options[i].textContent) {
8591
li.classList.add(selectedClass);

0 commit comments

Comments
 (0)