Facebook Pixel

OTP Input Auto-Focus Implementation in JavaScript

Auto-focus is the core feature. Here is how to implement it with forward and backward navigation.

OTP Input Auto-Focus Implementation in JavaScript

Auto-focus is the core feature of the OTP input. Here is how to implement forward and backward navigation.

Forward Navigation (Auto-Focus Next)

input.addEventListener("input", (e) => { const value = e.target.value.replace(/\D/g, ""); e.target.value = value; if (value && index < inputs.length - 1) { inputs[index + 1].focus(); } }); `` When the user types a digit, focus moves to the next input. The condition `index < inputs.length - 1` prevents focusing beyond the last input. ### Backward Navigation (Backspace) ```js input.addEventListener("keydown", (e) => { if (e.key === "Backspace" && !e.target.value && index > 0) { inputs[index - 1].focus(); } }); `` When the user presses Backspace on an empty input, focus moves to the previous input. The condition `!e.target.value` ensures we only go back when the current input is already empty (not when clearing a digit). ### Arrow Key Navigation ```js input.addEventListener("keyup", (e) => { if (e.key === "ArrowLeft" && index > 0) inputs[index - 1].focus(); if (e.key === "ArrowRight" && index < inputs.length - 1) inputs[index + 1].focus(); }); `` Arrow keys let the user navigate without typing. This improves UX for correction. ### Focus Styling ```css .otp-input:focus { border-color: #007bff; box-shadow: 0 0 0 3px rgba(0,123,255,0.2); } `` Visual feedback on focus is important for UX. The user needs to see which input is active. ### Edge Cases - Typing in a filled input: replace the existing value. - Tab key: let the browser handle default tab navigation. - Focus on click: clicking any input should focus it (default behavior). - Mobile: inputmode="numeric" shows the numeric keyboard. ### The Takeaway Auto-focus: on input, focus the next input (if not last). On Backspace (empty input), focus the previous. Arrow keys for manual navigation. CSS focus styling for visual feedback. Handle edge cases (filled inputs, tab key, mobile keyboard).

On the input event, if a value was entered and it is not the last input, focus the next input: if (value && index < inputs.length - 1) inputs[index + 1].focus(). This moves the cursor forward automatically.

On keydown, if the key is Backspace and the current input is empty (!e.target.value) and it is not the first input, focus the previous input: if (e.key === 'Backspace' && !e.target.value && index > 0) inputs[index - 1].focus().

Yes. Arrow Left and Arrow Right let the user navigate between inputs without typing. This improves UX for correction. On keyup, if ArrowLeft and index > 0, focus previous. If ArrowRight and not last, focus next.

Use CSS :focus and :focus-visible pseudo-classes. Change the border color and add a box-shadow: .otp-input:focus { border-color: #007bff; box-shadow: 0 0 0 3px rgba(0,123,255,0.2); }. This helps the user see which input is active.

Set inputmode='numeric' on the input element. This tells mobile browsers to show a numeric keyboard instead of the full keyboard. Also set pattern='[0-9]*' for older browsers.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.