How can I focus the next input once the previous input has reached its maxlength value?
a:
b:
You can use plain JavaScript:
See DEMO.
Check the character length with el.value.length
. If it is equal to the maximum value, move to the next field by using focus()
. Bind this function to the keyup event with onkeyup
so that the function fires every time after the user keys in a character.
var a = document.getElementById("a"),
b = document.getElementById("b"),
c = document.getElementById("c");
a.onkeyup = function() {
if (this.value.length === parseInt(this.attributes["maxlength"].value)) {
b.focus();
}
}
b.onkeyup = function() {
if (this.value.length === parseInt(this.attributes["maxlength"].value)) {
c.focus();
}
}