Skip to content

Commit 83d77cf

Browse files
committed
fix search field focussing problems. fixes select2#922
1 parent 9c2170c commit 83d77cf

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

select2.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,22 @@ the specific language governing permissions and limitations under the Apache Lic
229229

230230
$el.focus();
231231

232-
/* after the focus is set move the caret to the end, necessary when we val()
233-
just before setting focus */
234-
if(el.setSelectionRange)
235-
{
236-
el.setSelectionRange(pos, pos);
237-
}
238-
else if (el.createTextRange) {
239-
range = el.createTextRange();
240-
range.collapse(true);
241-
range.moveEnd('character', pos);
242-
range.moveStart('character', pos);
243-
range.select();
232+
/* make sure el received focus so we do not error out when trying to manipulate the caret.
233+
sometimes modals or others listeners may steal it after its set */
234+
if ($el.is(":visible") && el === document.activeElement) {
235+
236+
/* after the focus is set move the caret to the end, necessary when we val()
237+
just before setting focus */
238+
if(el.setSelectionRange)
239+
{
240+
el.setSelectionRange(pos, pos);
241+
}
242+
else if (el.createTextRange) {
243+
range = el.createTextRange();
244+
range.collapse(false);
245+
range.select();
246+
}
244247
}
245-
246248
}, 0);
247249
}
248250

0 commit comments

Comments
 (0)