var inputs = document.getElementsByTagName("input"), size = inputs.length; Array.prototype.forEach.call(inputs, function(a){ if (a.type == "text"){ a.addEventListener("keypress", function(e){ if (e.keyCode == 13) if (this.nextElementSibling == inputs[size]) inputs[0].focus(); else this.nextElementSibling.focus(); }, false); } });
Input 1: <input type = "text" autofocus /> Input 2: <input type = "text" /> Input 3: <input type = "text" /> Input 4: <input type = "text" /> Input 5: <input type = "text" />
input{ display: block; }