Moving a focus when the input text field reaches a max length

前端 未结 8 1534
北恋
北恋 2020-12-01 03:54

I do have a credit card number form. The number is divided into four parts just as on a real credit card.

I want to add a JavaScript taste to the form where when a u

8条回答
  •  盖世英雄少女心
    2020-12-01 04:26

    If your form fields are one beside the other like in your example, you could simply take advantage of nextElementSibling and voilà!

    function skipIfMax(element) {
      max = parseInt(element.dataset.max)
      
      
      if (element.value.length >= max && element.nextElementSibling) {
        element.nextElementSibling.focus();  
      }
    }
    
    
    
    

提交回复
热议问题