Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Focus on first matching item when key is pressed #1363

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions js/selectlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@
if (options.resize === 'auto' || this.$element.attr('data-resize') === 'auto') {
this.resize();
}

// support jumping focus to first letter in dropdown when key is pressed
this.$element.on('shown.bs.dropdown', function () {
var $this = $(this);
// attach key listener when dropdown is shown
$(document).on('keypress.fu.selectlist', function(e){

// get the key that was pressed
var key = String.fromCharCode(e.which);
// look the items to find the first item with the first character match and set focus
$this.find("li").each(function(idx,item){
if ($(item).text().charAt(0).toLowerCase() === key) {
$(item).children('a').focus();
return false;
}
});

});
});

// unbind key event when dropdown is hidden
this.$element.on('hide.bs.dropdown', function () {
$(document).off('keypress.fu.selectlist');
});
};

Selectlist.prototype = {
Expand Down