Vue Js Password Show Hide
In this tutorial, we will learn how to toggle the visibility of passwords in input fields by changing the input field's type attribute from 'password' to 'text' using Vue.js.

written

reviewed

updated

Thanks for your feedback!
Your contributions will help us to improve service.
How to Add an Eye Icon to a Password Field Using Vue.js
The Vue.js code below demonstrates how to show or hide a password in an input field by clicking a button. See the example below and edit the code using the 'Tryit' editor.
Vue Password Input
Copied to Clipboard
x
<div id="app">
<div class="input-group">
<input :type="showPassword ? 'text' : 'password'" v-model="password" placeholder="Enter your password">
<i class="icon " :class="showPassword ? 'fa-solid fa-eye-slash' : 'fa-solid fa-eye'"
@click="showPassword = !showPassword"></i>
</div>
</div>
<script type="module">
const app = Vue.createApp({
data() {
return {
password: '',
showPassword: false
};
}
});
app.mount("#app");
</script>
Output of Vue Show Password Button Inside Input
Example 2
Copied to Clipboard
xxxxxxxxxx
<template>
<div>
<input :type="passwordFieldType" v-model="password" placeholder="Enter your password" />
<button @click="togglePasswordVisibility">
{{ passwordFieldType === 'password' ? 'Show' : 'Hide' }} Password
</button>
</div>
</template>
<script>
export default {
data() {
return {
password: '',
isPasswordVisible: false,
};
},
computed: {
passwordFieldType() {
return this.isPasswordVisible ? 'text' : 'password';
},
},
methods: {
togglePasswordVisibility() {
this.isPasswordVisible = !this.isPasswordVisible;
},
},
};
</script>
Ad