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.
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.
<div 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" aria-label="password toggle">
<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>
Password input with label.
<div>
<label class="label label-text" for="toggle-password-label"> Password </label>
<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" aria-label="password toggle">
<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 example with input type.
<!-- Floating example -->
<div class="input-group max-w-sm">
<div class="relative 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" for="toggle-password-floating">Password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating" }' class="block" aria-label="password toggle">
<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>
<!-- Filled example -->
<div class="max-w-sm">
<div class="input-group-filled">
<div class="relative 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" for="toggle-password-filled">Enter password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-filled" }' class="block" aria-label="password toggle">
<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>
<span class="input-filled-focused"></span>
</div>
Success state can be show using is-valid
class.
<!-- Basis -->
<div>
<label class="label label-text" for="toggle-password-basic-success"> Password </label>
<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" aria-label="password toggle">
<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>
<!-- Floating example -->
<div class="input-group max-w-sm">
<div class="relative 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" for="toggle-password-floating-success">Password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating-success" }' class="block" aria-label="password toggle">
<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>
<!-- Filled example -->
<div class="max-w-sm">
<div class="input-group-filled">
<div class="relative 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" for="toggle-password-filled-success">Enter password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-filled-success" }' class="block" aria-label="password toggle">
<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>
<span class="input-filled-focused"></span>
</div>
Error state can be show using is-invalid
class.
<!-- Basis -->
<div>
<label class="label label-text" for="toggle-password-basic-error"> Password </label>
<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" aria-label="password toggle">
<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>
<!-- Floating example -->
<div class="input-group max-w-sm">
<div class="relative 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" for="toggle-password-floating-error">Password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-floating-error" }' class="block" aria-label="password toggle">
<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>
<!-- Filled example -->
<div class="max-w-sm">
<div class="input-group-filled">
<div class="relative 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" for="toggle-password-filled-error">Enter password</label>
</div>
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": "#toggle-password-filled-error" }' class="block" aria-label="password toggle">
<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>
<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 label-text" for="toggle-password-multiple1"> Current Password </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" aria-label="password toggle">
<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 label-text" for="toggle-password-multiple2"> New Password </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" aria-label="password toggle">
<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.
<div class="mb-3 max-w-sm">
<label class="label 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 gap-2 items-center">
<input data-toggle-password='{ "target": "#toggle-password-checkbox" }' type="checkbox" class="checkbox checkbox-primary" id="toggleChecboxPassword" />
<label class="label label-text text-base" for="toggleChecboxPassword">Show password</label>
</div>
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 label-text" for="toggle-password-modal"> Password </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" aria-label="password toggle">
<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>
destroy
method is provided to facilitate the destruction of a toggle password.<div id="toggle-password-to-destroy" data-toggle-password-group>
<label class="label label-text" for="toggle-password-destroy"> Current Password </label>
<div class="input-group mb-4 max-w-sm">
<input id="toggle-password-destroy" 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-destroy","#toggle-password-destroy2"] }' class="block" aria-label="password toggle">
<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 label-text" for="toggle-password-destroy2"> New Password </label>
<div class="input-group max-w-sm">
<input id="toggle-password-destroy2" type="password" class="input" placeholder="Enter password" />
<span class="input-group-text">
<button type="button" data-toggle-password='{ "target": ["#toggle-password-destroy","#toggle-password-destroy2"] }' class="block" aria-label="password toggle">
<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="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', () => {
})
</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-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" aria-label="password toggle">
<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>
<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();
});
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-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" aria-label="password toggle">
<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>
<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)});