Currently in the process of creating a Password field that allows users to hide or display their password in React/Ionic.
- I am aiming to maintain focus on the password field when switching the input type, similar to how it is done on the PayPal Login page. I achieve this by using
event.preventDefault()
on themousedown
event.
const input = document.querySelector('input')
function toggleVisibility() {
if (input.type === "text")
input.type = "password"
else
input.type = "text"
}
function keepFocus(event) {
event.preventDefault()
}
<input placeholder="Password" type="text">
<button onmousedown="keepFocus(event)" onclick="toggleVisibility()">Show Password</button>
Issue
- After switching the input field type, the cursor jumps back to the beginning of the input field. I would like to retain the cursor position as it was before the change. (Similar to the behavior on PayPal)