27 lines
1020 B
JavaScript
27 lines
1020 B
JavaScript
(function () {
|
|
const toggles = document.querySelectorAll('[data-password-toggle]');
|
|
|
|
toggles.forEach((toggle) => {
|
|
if (!(toggle instanceof HTMLButtonElement)) {
|
|
return;
|
|
}
|
|
|
|
const controlId = toggle.getAttribute('aria-controls');
|
|
const input = controlId ? document.getElementById(controlId) : toggle.parentElement?.querySelector('input[type="password"], input[type="text"]');
|
|
|
|
if (!(input instanceof HTMLInputElement)) {
|
|
return;
|
|
}
|
|
|
|
const showLabel = toggle.getAttribute('data-label-show') || 'Passwort anzeigen';
|
|
const hideLabel = toggle.getAttribute('data-label-hide') || 'Passwort verbergen';
|
|
|
|
toggle.addEventListener('click', () => {
|
|
const isHidden = input.type === 'password';
|
|
input.type = isHidden ? 'text' : 'password';
|
|
toggle.classList.toggle('is-visible', isHidden);
|
|
toggle.setAttribute('aria-label', isHidden ? hideLabel : showLabel);
|
|
});
|
|
});
|
|
}());
|