advanced forms
Tailwind CSS Toggle Password
The Toggle Password Component allows users to show or hide their password in forms, helping ensure accuracy while keeping sensitive information secure.
Basic example of toggle password.
<div class="input max-w-sm">
<input id="toggle-password" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": "#toggle-password" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
Password input with label.
<div class="max-w-sm">
<label class="label-text" for="toggle-password-label">Password</label>
<div class="input">
<input id="toggle-password-label" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": "#toggle-password-label" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
Toggle password example with input type.
<div class="input max-w-sm">
<div class="input-floating grow">
<input id="toggle-password-floating" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-floating-label ms-0" for="toggle-password-floating">Password</label>
</div>
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating" }' class="block cursor-pointer" aria-label="password toggle">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
Success state can be show using is-valid
class.
<!-- Basis -->
<div class="max-w-sm">
<label class="label-text" for="toggle-password-basic-success">Password</label>
<div class="input is-valid">
<input id="toggle-password-basic-success" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": "#toggle-password-basic-success" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
<!-- Floating example -->
<div class="input max-w-sm">
<div class="input-floating">
<input id="toggle-password-floating-success" class="is-valid" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-floating-label ms-0" for="toggle-password-floating-success">Password</label>
</div>
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating-success" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
Error state can be show using is-invalid
class.
<!-- Basis -->
<div class="max-w-sm">
<label class="label-text" for="toggle-password-basic-error">Password</label>
<div class="input is-invalid">
<input id="toggle-password-basic-error" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": "#toggle-password-basic-error" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
<!-- Floating example -->
<div class="input max-w-sm">
<div class="input-floating">
<input id="toggle-password-floating-error" class="is-invalid" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<label class="input-floating-label ms-0" for="toggle-password-floating-error">Password</label>
</div>
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating-error" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
Utilize the data-toggle-password-group
attribute to consolidate multiple passwords under a single toggle icon.
<div data-toggle-password-group>
<!-- Current password -->
<div class="mb-4 max-w-sm">
<label class="label-text" for="toggle-password-multiple1">Current Password</label>
<div class="input">
<input id="toggle-password-multiple1" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": ["#toggle-password-multiple1","#toggle-password-multiple2"] }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
<!-- New password -->
<div class="max-w-sm">
<label class="label-text" for="toggle-password-multiple2">New Password</label>
<div class="input">
<input id="toggle-password-multiple2" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": ["#toggle-password-multiple1","#toggle-password-multiple2"] }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
</div>
Toggle password visibility with a checkbox.
<div class="mb-3 max-w-sm">
<label class="label-text" for="toggle-password-checkbox">Password</label>
<input id="toggle-password-checkbox" type="password" class="input" placeholder="Enter password" value="Pwd_1242@mA1" />
</div>
<div class="flex items-center gap-2">
<input id="toggleCheckboxPassword" type="checkbox" data-toggle-password='{ "target": "#toggle-password-checkbox" }' class="checkbox checkbox-primary" />
<label class="label-text text-base" for="toggleCheckboxPassword">Show password</label>
</div>
Basic usage in modal window.
<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 overlay-open:duration-300 hidden" role="dialog" tabindex="-1">
<div class="modal-dialog overlay-open:opacity-100 overlay-open:duration-300">
<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-text" for="toggle-password-modal">Password</label>
<div class="input">
<input id="toggle-password-modal" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": "#toggle-password-modal" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span
class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"
></span>
</button>
</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>
destroy
method is provided to facilitate the destruction of a toggle password.<div id="toggle-password-to-destroy" data-toggle-password-group>
<!-- Current password -->
<div class="mb-4 max-w-sm">
<label class="label-text" for="toggle-password-destroy">Current Password</label>
<div class="input">
<input id="toggle-password-destroy" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": ["#toggle-password-destroy","#toggle-password-destroy2"] }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
<!-- New password -->
<div class="max-w-sm">
<label class="label-text" for="toggle-password-destroy2">New Password</label>
<div class="input">
<input id="toggle-password-destroy2" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button type="button" data-toggle-password='{ "target": ["#toggle-password-destroy","#toggle-password-destroy2"] }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
</div>
</div>
<div class="mt-4 flex gap-3">
<button class="btn btn-primary" id="destroy-btn">Destroy</button>
<button class="btn btn-primary" id="reinit-btn" disabled>Reinitialize</button>
</div>
<script>
window.addEventListener('load', () => {
// Destroy and reinit variables
const togglesPassword = document.querySelectorAll('#toggle-password-to-destroy [data-toggle-password]')
const destroyBtn = document.querySelector('#destroy-btn')
const reinitBtn = document.querySelector('#reinit-btn')
destroyBtn.addEventListener('click', () => {
togglesPassword.forEach(el => {
const { element } = HSTogglePassword.getInstance(el, true)
element.destroy()
})
destroyBtn.setAttribute('disabled', 'disabled')
reinitBtn.removeAttribute('disabled')
})
reinitBtn.addEventListener('click', () => {
HSTogglePassword.autoInit()
reinitBtn.setAttribute('disabled', 'disabled')
destroyBtn.removeAttribute('disabled')
})
})
</script>
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. |
destroy() | Destroys the instance, removes generated markup (if any), removes added classes and attributes. |
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.
<div class="input max-w-sm">
<input id="toggle-password-mt" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button id="toggle-password-method" type="button" data-toggle-password='{ "target": "#toggle-password-mt" }' class="block cursor-pointer" aria-label="password toggle">
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
<button type="button" class="btn btn-primary w-max" 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();
});
Destroy instance.
const { element } = HSTogglePassword.getInstance('#toggle-password-to-destroy', true);
const destroyBtn = document.querySelector('#destroy-btn');
destroyBtn.addEventListener('click', () => {
element.destroy();
});
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
.
<div class="input max-w-sm">
<input id="toggle-password-el" type="password" placeholder="Enter password" value="Pwd_1242@mA1" />
<button id="toggle-password-event" type="button" data-toggle-password='{ "target": "#toggle-password-el" }' class="block cursor-pointer" aria-label="password toggle" >
<span class="icon-[tabler--eye] text-base-content/80 password-active:block hidden size-5 shrink-0"></span>
<span class="icon-[tabler--eye-off] text-base-content/80 password-active:hidden block size-5 shrink-0"></span>
</button>
</div>
<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)});