From 49365915d198c8b115ce683684ba8505efa8f039 Mon Sep 17 00:00:00 2001 From: Stephen James Date: Fri, 19 Jun 2015 15:13:59 -0400 Subject: [PATCH] Selectlist: focus on first matching when key pressed --- js/selectlist.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/js/selectlist.js b/js/selectlist.js index f3643c765..3358988a5 100644 --- a/js/selectlist.js +++ b/js/selectlist.js @@ -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 = {