<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> <style id="jsbin-css"> * { margin: 0; padding: 0; } .box { float: left; margin: 50px 10px; position: relative; width: 30px; height: 30px; border-bottom: 5px solid #fcc; } input { border: 0; position: absolute; top: 0; left: 0; z-index:999; width: 100%; height: 100%; text-align: center; background: transparent; opacity: 0; } input:focus { outline: 0; } input:valid { opacity: 1; } input:focus ~ .circle, input:valid ~ .circle { display: none; } .circle { position: absolute; top: 5px; left: 5px; width: 20px; height: 20px; border-radius: 50%; background: #fcc; } </style> </head> <body> <div class="box"> <input type="text" maxlength="1" required="required"> <div class="circle"></div> </div> <div class="box"> <input type="text" maxlength="1" required="required"> <div class="circle"></div> </div> <div class="box"> <input type="text" maxlength="1" required="required"> <div class="circle"></div> </div> <div class="box"> <input type="text" maxlength="1" required="required"> <div class="circle"></div> </div> <div class="box"> <input type="text" maxlength="1" required="required"> <div class="circle"></div> </div> <script id="jsbin-javascript"> const nodes = document.querySelectorAll('.box>input') Array .from(nodes) .map((item, index) => { item.addEventListener('keyup', (event) => { let target if (event.code === 'Backspace' && index > 0) { target = item.parentNode.previousElementSibling.children[0] target.value = '' target.focus() } else if (item.value != null && item.value !== '' && index < 4) { target = item.parentNode.nextElementSibling.children[0] target.focus() } else {} }) }) </script> <script id="jsbin-source-css" type="text/css">* { margin: 0; padding: 0; } .box { float: left; margin: 50px 10px; position: relative; width: 30px; height: 30px; border-bottom: 5px solid #fcc; } input { border: 0; position: absolute; top: 0; left: 0; z-index:999; width: 100%; height: 100%; text-align: center; background: transparent; opacity: 0; } input:focus { outline: 0; } input:valid { opacity: 1; } input:focus ~ .circle, input:valid ~ .circle { display: none; } .circle { position: absolute; top: 5px; left: 5px; width: 20px; height: 20px; border-radius: 50%; background: #fcc; }</script> <script id="jsbin-source-javascript" type="text/javascript">const nodes = document.querySelectorAll('.box>input') Array .from(nodes) .map((item, index) => { item.addEventListener('keyup', (event) => { let target if (event.code === 'Backspace' && index > 0) { target = item.parentNode.previousElementSibling.children[0] target.value = '' target.focus() } else if (item.value != null && item.value !== '' && index < 4) { target = item.parentNode.nextElementSibling.children[0] target.focus() } else {} }) }) </script></body> </html>