Skip to content

Commit 3f62d88

Browse files
committed
[Fix] angular-ui#317: Applying ui-mask on multiple fields on a form sets focus on the last masked input box automatically
1 parent 6ea34c8 commit 3f62d88

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

modules/mask/mask.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,13 @@ angular.module('ui.mask', [])
437437
if (input.selectionStart !== undefined) {
438438
return input.selectionStart;
439439
} else if (document.selection) {
440-
// Curse you IE
441-
input.focus();
442-
var selection = document.selection.createRange();
443-
selection.moveStart('character', input.value ? -input.value.length : 0);
444-
return selection.text.length;
440+
if ($(input).is(':focus')) {
441+
// Curse you IE
442+
input.focus();
443+
var selection = document.selection.createRange();
444+
selection.moveStart('character', input.value ? -input.value.length : 0);
445+
return selection.text.length;
446+
}
445447
}
446448
return 0;
447449
}
@@ -452,16 +454,20 @@ angular.module('ui.mask', [])
452454
return; // Input's hidden
453455
}
454456
if (input.setSelectionRange) {
455-
input.focus();
456-
input.setSelectionRange(pos, pos);
457+
if ($(input).is(':focus')) {
458+
input.focus();
459+
input.setSelectionRange(pos, pos);
460+
}
457461
}
458462
else if (input.createTextRange) {
459-
// Curse you IE
460-
var range = input.createTextRange();
461-
range.collapse(true);
462-
range.moveEnd('character', pos);
463-
range.moveStart('character', pos);
464-
range.select();
463+
if ($(input).is(':focus')) {
464+
// Curse you IE
465+
var range = input.createTextRange();
466+
range.collapse(true);
467+
range.moveEnd('character', pos);
468+
range.moveStart('character', pos);
469+
range.select();
470+
}
465471
}
466472
}
467473

0 commit comments

Comments
 (0)