-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtogglepassword.js
39 lines (33 loc) · 1.55 KB
/
togglepassword.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Bootstrap 5 Toggle Password
* Author: Rudrian Riener Pariñas
* Description: Toggle password visibility
*/
(function ($) {
$.fn.togglepassword = function (btnclass) {
this.each(function(){
let id = $(this).attr('id') ?? ($(this).attr('name') ?? '');
let autofocus = $(this).prop('autofocus') !== true ? '' : ' autofocus';
let name = $(this).attr('name') ?? '';
let classlist = $(this).attr('class') ?? '';
let value = $(this).val();
let content = '<i class="bi bi-eye-slash"></i>';
let title = 'Show password';
btnclass = btnclass ?? '';
return $(this).replaceWith(
$('<div class="d-flex"></div>')
.append($('<input type="password" class="' + classlist + '" id="' + id + '" name="' + name + '" value="' + value + '"' + autofocus + '>'))
.append('<button type="button" class="' + btnclass + '" data-role="togglepassword" data-target="#' + id + '" title="' + title + '" tabindex="-1">' + content + '</button>')
);
});
};
})(jQuery);
$(() => {
$(document).on('click', '[data-role="togglepassword"]', function () {
let target = $(this).data('target');
let is_password = $(target).attr('type') === 'password';
$(target).attr('type', (is_password ? 'text' : 'password'));
$(this).html('<i class="bi bi-eye' + (is_password ? '' : '-slash') + '"></i>');
$(this).attr('title', is_password ? 'Show password' : 'Hide password');
});
});