const pinLogin = document.createElement('template'); pinLogin.innerHTML = `
`; customElements.define('login-pin', class extends HTMLElement { constructor() { super() this.attachShadow({ mode: 'open' }).append(pinLogin.content.cloneNode(true)) this.pin_block = []; this.container = this.shadowRoot.querySelector('.pin-container'); } renderBlocks(){ const frag = document.createDocumentFragment(); for(let i=0 ; i<4;i++){ const password = document.createElement('input'); password.classList.add('input-style'); password.setAttribute('type', "password"); password.setAttribute('maxlength', "1"); this.pin_block.push(password); frag.append(password); } this.container.append(frag); } connectedCallback() { this.renderBlocks(); this.pin_block[0].focus(); let pin_block = this.pin_block; let count =0; this.pin_block.forEach((input) =>{ input.addEventListener('keydown' , function(e){ if(e.keyCode >=48 && e.keyCode<= 57){ if(count>4){ e.preventDefault(); } else{ pin_block[count].classList.add('input-pin'); pin_block[count++].focus(); } } if(e.key === "Backspace"){ if(count <= 0){ e.preventDefault(); } else{ count--; pin_block[count].classList.remove('input-pin'); pin_block[count].focus(); } } }); }); } });