Created
June 12, 2023 15:29
-
-
Save neverything/9a28e2c5d1621972ed5c587772f37be0 to your computer and use it in GitHub Desktop.
Toggle the visibility on a password input field with Alpine.js: https://silvanhagen.com/toggle-password-visibility-alpinejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Define the Alpine.js component using x-data --> | |
<div x-data="{ show: false }"> | |
<!-- Bind the type attribute to the value of show using the :type notation --> | |
<input :type="show ? 'text' : 'password'" name="password" type="password" value="LookMaaGreatPassw0rd!" /> | |
<!-- Toggle the value of show on click using @click and bind the class | |
attribute to it using the :class notation --> | |
<svg @click="show = !show" :class="{'block': !show, 'hidden':show }">...</svg> | |
<!-- And the same goes for the svg hidden by default. --> | |
<svg @click="show = !show" :class="{'hidden': !show, 'block':show }">...</svg> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment