Skip to content

Instantly share code, notes, and snippets.

@mroien
Created September 2, 2016 03:04
Show Gist options
  • Save mroien/9bce7e442b0f525552850973890b97aa to your computer and use it in GitHub Desktop.
Save mroien/9bce7e442b0f525552850973890b97aa to your computer and use it in GitHub Desktop.
Auto change focus after maxlength
var container = document.getElementsByTagName("form")[0];
container.onkeyup = function(e) {
var target = e.srcElement;
var maxLength = parseInt(target.attributes["maxlength"].value, 10);
var myLength = target.value.length;
if (myLength >= maxLength) {
var next = target;
while (next = next.nextElementSibling) {
if (next == null)
break;
if (next.tagName.toLowerCase() == "input") {
next.focus();
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment