From 2e79f5eb4fd75464d255835374259ab762ba4c7a Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sun, 20 Apr 2014 13:42:43 -0400 Subject: [PATCH] Only disable keyboard focusing for touch devices [Fixes #1541] This fixes the issue [1] by first checking to see if the current device is a touch device. The other issue [2] that occured because of the original fix [3] is now fixed, because the hidden inputs are always focused by default on non-touch devices. The code used for detecting touch devices was pulled from StackOverflow [4]. Information on the reasoning behind this fix can be found on GitHub [5]. [1]: https://github.com/ivaynberg/select2/issues/1541 [2]: https://github.com/ivaynberg/select2/issues/2223 [3]: https://github.com/ivaynberg/select2/commit/d87e93dd45ade99e7894ac4854bf65e8f59667d4 [4]: http://stackoverflow.com/a/15439809/359284 [5]: https://github.com/ivaynberg/select2/issues/1541#issuecomment-39805859 --- select2.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/select2.js b/select2.js index ea175cf7..4331225d 100644 --- a/select2.js +++ b/select2.js @@ -3384,6 +3384,15 @@ the specific language governing permissions and limitations under the Apache Lic searchInputPlaceholder: '', createSearchChoicePosition: 'top', shouldFocusInput: function (instance) { + // Attempt to detect touch devices + var supportsTouchEvents = (('ontouchstart' in window) || + (navigator.msMaxTouchPoints > 0)); + + // Only devices which support touch events should be special cased + if (!supportsTouchEvents) { + return true; + } + // Never focus the input if search is disabled if (instance.opts.minimumResultsForSearch < 0) { return false;