Skip to content

Instantly share code, notes, and snippets.

@almirus
Created July 28, 2018 10:25
Show Gist options
  • Save almirus/8b190c7a4e5079f46f1c9e5afa3a4964 to your computer and use it in GitHub Desktop.
Save almirus/8b190c7a4e5079f46f1c9e5afa3a4964 to your computer and use it in GitHub Desktop.
div.onkeyup = function(e) {
var target = e.srcElement;
var keyCode = ('which' in event) ? e.which : e.keyCode;
var maxLength = parseInt(target.attributes["maxlength"].value, 10);
var position = target.selectionStart;
var myLength = target.value.length;
// если достигли при вводе макс длины и это не кнопки влево\вправо и курсор в конце строки и нажали вправо
if ((myLength >= maxLength && keyCode!==37 && keyCode!==39) || (position===myLength && keyCode===39)) {
var next = target;
// берем след элемент
while (next = next.nextElementSibling) {
if (next == null)
break;
// если это инпут
if (next.tagName.toLowerCase() === "input" && next.type!=="hidden") {
next.focus(); // ставим курсор
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment