-1

Following code moves the cursor from one textbox to next once it meets the maxlength criteria. Following code works very well in Chrome. For the first textbox max length is 3 so cursor automatically moves to next textbox. But do not understand why in internet explorer it does not do so. Please help, I am missing the important delivery date. Please note that 'mobNum' is the class of those textboxes

$('.mobNum').keypress(function(evt) {
    debugger;
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    $("#msgMobileValidation").text("");
    $("#divClientValidMobile").hide();
    var idStr = $(this).attr('id').split('_')[0];
    var idCnt = $(this).attr('id').split('_')[1];
    var nextIdCnt = parseInt(idCnt) + 1;
    nextIdCnt = nextIdCnt.toString();
    var nextControlId = "#" + idStr + "_" + nextIdCnt;
    var strLength = this.value.length + 1;
    var limit = parseInt($(this).attr('maxlength'));
    if (strLength == limit) {
        //$(nextControlId).focus();
        document.getElementById(nextControlId).focus();
    }
});
Toto
  • 89,455
  • 62
  • 89
  • 125
joy
  • 47
  • 1
  • 5
  • 1
    Possible duplicate of [focus doesn't work in IE](http://stackoverflow.com/questions/2600186/focus-doesnt-work-in-ie) & http://stackoverflow.com/questions/31947590/unable-to-focus-an-input-using-javascript-in-ie11 – Darshak Mar 01 '17 at 13:35

1 Answers1

0

For IE you need to implement focus using setTimeout, So replace the line:

document.getElementById(nextControlId).focus();

with this one:

setTimeout(function(){document.getElementById(nextControlId).focus();},20);

Note that you can put any value instead of 20 (its time in miliseconds), but make sure it doesn't cause latency. so keep it small (5, 10, 15, 20...)

behkod
  • 2,647
  • 2
  • 18
  • 33