Selaa lähdekoodia

fix search field focussing problems. fixes #922

Igor Vaynberg 12 vuotta sitten
vanhempi
commit
83d77cfa62
1 muutettua tiedostoa jossa 15 lisäystä ja 13 poistoa
  1. 15 13
      select2.js

+ 15 - 13
select2.js

@@ -229,20 +229,22 @@ the specific language governing permissions and limitations under the Apache Lic
 
             $el.focus();
 
-            /* after the focus is set move the caret to the end, necessary when we val()
-                just before setting focus */
-            if(el.setSelectionRange)
-            {
-                el.setSelectionRange(pos, pos);
-            }
-            else if (el.createTextRange) {
-                range = el.createTextRange();
-                range.collapse(true);
-                range.moveEnd('character', pos);
-                range.moveStart('character', pos);
-                range.select();
+            /* make sure el received focus so we do not error out when trying to manipulate the caret.
+                sometimes modals or others listeners may steal it after its set */
+            if ($el.is(":visible") && el === document.activeElement) {
+
+                /* after the focus is set move the caret to the end, necessary when we val()
+                    just before setting focus */
+                if(el.setSelectionRange)
+                {
+                    el.setSelectionRange(pos, pos);
+                }
+                else if (el.createTextRange) {
+                    range = el.createTextRange();
+                    range.collapse(false);
+                    range.select();
+                }
             }
-
         }, 0);
     }