HTML

<input id="&quot;password-input&quot;" type="&quot;password&quot;" />
<button class="&quot;toggle-password&quot;">Toggle PW</button>

JavaScript

// Select the Password Input Field
const passwordInput = document.querySelector( "#password-input" );

// Select the Show / Hide Password Button
const togglePasswordButton = document.querySelector( ".toggle-password" );

togglePasswordButton.addEventListener( "click", () => {

    // Check for the current type of the input
    if( passwordInput.type === "password" ) {

        // Switch up the type of the input
        passwordInput.type = "text";
    }
    else if( passwordInput.type === "text" ) {

        passwordInput.type = "password";
    }
});

Result

Back To TIL