JavaScript Form Focus Automatic Switch Code

  • 2021-07-04 18:17:40
  • OfStack

Needless to say, the key code is as follows:


--- Restore content start ---

<html>
    <head>
          <script>
window.onload=function(){
                var form=document.getElementsByTagName('form')[0];
                var txt=form.elements['txt1'];
                var txt1=form.elements['txt2'];
             txt.onkeyup=function(){
                if(this.value.length==this.maxLength){
                      for(var i=0;i<form.elements.length;i++){
                            if(form.elements[i]==this){
                            form.elements[i+1].focus();
                      }    
                }
          }
    }
}
          </script>
    </head>
    <body>
          <form>
                <input type="text" name="txt1" maxlength="5">
                <input type="text" name="txt2" maxlength="4">
          </form>
    </body>
</html>
--- End of Recovery Content ---

The above is this site to introduce the JavaScript form focus automatic switching code, I hope to help you!


Related articles: