advanced forms
Tailwind CSS Toggle Password
The Toggle Password Component lets users show or hide their password in login or registration forms. It helps them check for accuracy while still keeping sensitive information secure.
Class Name | Type | Description |
---|---|---|
input | Component | Basic input field. |
password-active:{tw-utility-class} | Modifier | A method to apply specific Tailwind classes when the password is visible. |
is-valid | Modifier | Adds success style to the input. |
is-invalid | Modifier | Adds error style to the input. |
Basic example of toggle password.
<label class="input-group max-w-sm">
<input id="toggle-password" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</label>
Password input with label.
<label>
<div class="label">
<span class="label-text">Password</span>
</div>
<div class="input-group max-w-sm">
<input id="toggle-password-label" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-label" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</div>
</label>
Toggle password example with input type.
<!-- Floating example -->
<label class="input-group max-w-sm">
<div class="form-control w-full">
<input id="toggle-password-floating" type="password" class="input input-floating peer" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-floating-label">Password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</label>
<!-- Filled example -->
<div class="max-w-sm">
<label class="input-group-filled">
<div class="form-control w-full">
<input type="password" id="toggle-password-filled" class="input input-filled peer" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-filled-label">Enter password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-filled" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</label>
<span class="input-filled-focused"></span>
</div>
Success state can be show using is-valid
class.
<!-- Basis -->
<label>
<div class="label">
<span class="label-text">Password</span>
</div>
<div class="input-group max-w-sm">
<input id="toggle-password-basic-success" type="password" class="input is-valid" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-basic-success" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</div>
</label>
<!-- Floating example -->
<label class="input-group max-w-sm">
<div class="form-control w-full">
<input id="toggle-password-floating-success" type="password" class="input is-valid input-floating peer" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-floating-label">Password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating-success" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</label>
<!-- Filled example -->
<div class="max-w-sm">
<label class="input-group-filled">
<div class="form-control w-full">
<input type="password" id="toggle-password-filled-success" class="input is-valid input-filled peer" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-filled-label">Enter password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-filled-success" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</label>
<span class="input-filled-focused"></span>
</div>
Error state can be show using is-invalid
class.
<!-- Basis -->
<label>
<div class="label">
<span class="label-text">Password</span>
</div>
<div class="input-group max-w-sm">
<input id="toggle-password-basic-error" type="password" class="input is-invalid" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-basic-error" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</div>
</label>
<!-- Floating example -->
<label class="input-group max-w-sm">
<div class="form-control w-full">
<input id="toggle-password-floating-error" type="password" class="input is-invalid input-floating peer" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-floating-label">Password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating-error" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</label>
<!-- Filled example -->
<div class="max-w-sm">
<label class="input-group-filled">
<div class="form-control w-full">
<input type="password" id="toggle-password-filled-error" class="input is-invalid input-filled peer" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-filled-label">Enter password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-filled-error" }' class="block" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</label>
<span class="input-filled-focused"></span>
</div>
Utilize the data-toggle-password-group
attribute to consolidate multiple passwords under a single toggle icon.
<div data-toggle-password-group>
<label class="label" for="toggle-password-multiple1">
<span class="label-text">Current Password</span>
</label>
<div class="input-group mb-4 max-w-sm">
<input id="toggle-password-multiple1" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": ["#toggle-password-multiple1","#toggle-password-multiple2"] }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</div>
<!-- New password -->
<label class="label" for="toggle-password-multiple2">
<span class="label-text">New Password</span>
</label>
<div class="input-group max-w-sm">
<input id="toggle-password-multiple2" type="password" class="input" placeholder="Enter password" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": ["#toggle-password-multiple1","#toggle-password-multiple2"] }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"> </span>
</button>
</span>
</div>
</div>
Toggle password visibility with a checkbox.
<label>
<span class="label">
<span class="label-text">Password</span>
</span>
<div class="input-group mb-3 max-w-sm">
<input id="toggle-password-checkbox" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
</div>
</label>
<label class="form-control flex gap-2 items-center">
<input data-toggle-password='{ "target": "#toggle-password-checkbox" }' type="checkbox" class="checkbox checkbox-primary" />
<span class="label-text ">Show password</span>
</label>
Basic usage in modal window.
Check Password
<button type="button" class="btn btn-primary" aria-haspopup="dialog" aria-expanded="false" aria-controls="password-modal" data-overlay="#password-modal">Open modal</button>
<div id="password-modal" class="overlay modal overlay-open:opacity-100 hidden" role="dialog" tabindex="-1">
<div class="modal-dialog overlay-open:opacity-100">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Check Password</h3>
<button type="button" class="btn btn-text btn-circle btn-sm absolute end-3 top-3" aria-label="Close" data-overlay="#password-modal">
<span class="icon-[tabler--x] size-4"></span>
</button>
</div>
<div class="modal-body">
<label class="label" for="toggle-password-modal">
<span class="label-text">Password</span>
</label>
<div class="input-group">
<input id="toggle-password-modal" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-modal" }' class="block">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0" ></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0" ></span>
</button>
</span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-soft btn-secondary" data-overlay="#password-modal">Close</button>
</div>
</div>
</div>
</div>
PARAMETERS | DESCRIPTION | OPTIONS | DEFAULT VALUE |
---|---|---|---|
data-toggle-password | Enable a Toggle Password feature by specifying it on an element. | - | - |
:target (required) | This function accepts the ID of the content element that should be toggle, typically the ID of an input element. | string | - |
:isShown | Determines password visibility. | boolean | false |
:eventType | Determines event type. | change or click | click |
data-toggle-password-group | This feature enables you to group multiple toggle password elements together, allowing them to be toggled simultaneously. It should be applied to the parent element containing the toggle password elements. | - | - |
The HSTogglePassword
object is contained within the global window
object.
METHOD | DESCRIPTION |
---|---|
PUBLIC METHODS | |
show() | Toggle the field to type text. |
hide() | Toggle the field to type password. |
STATIC METHODS | |
HSTogglePassword.getInstance(target, isInstance) | Returns the element linked to the target .
|
Assign an ID to the div containing the data-toggle-password
attribute, as demonstrated in the public method. To explore additional methods, you can copy them into the console.
<label class="input-group mb-4 max-w-sm">
<input id="toggle-password-mt" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-mt" }' class="block" id="toggle-password-method">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</label>
<button type="button" class="btn btn-primary" id="show-btn">Show Btn</button>
<script>
window.addEventListener('load', function () {
const togglePassword = new HSTogglePassword(document.querySelector('#toggle-password-method'))
const showBtn = document.querySelector('#show-btn')
showBtn.addEventListener('click', () => {
togglePassword.show()
})
})
</script>
Toggle the field to type text (public method).
// This method is used in above example.
const togglePassword = new HSTogglePassword(document.querySelector('#toggle-password-method'));
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
togglePassword.show();
});
Toggle the field to type text (mixed).
const { element } = HSTogglePassword.getInstance('#toggle-password-method', true);
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
element.show();
});
METHOD | DESCRIPTION | RETURNING VALUE |
---|---|---|
on:toggle | Called when the password is shown or hidden. | Target |
Assign an ID to the div that contains the data-toggle-password
attribute, and then use that ID to attach an event.
The example below demonstrates a use case of event usage. You can view the output in the console by typing in input
.
<label class="input-group max-w-sm">
<input id="toggle-password-el" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-el" }' class="block" id="toggle-password-event">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 flex-shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 flex-shrink-0"></span>
</button>
</span>
</label>
<script>
window.addEventListener('load', function () {
const el = HSTogglePassword.getInstance('#toggle-password-event')
el.on('toggle', target => {
console.log('target:', target)
})
})
</script>
An example where a function is run every time a field changes visibility.
const el = HSTogglePassword.getInstance('#toggle-password-event');
el.on('toggle', (target) => {console.log('target:', target)});