I need to add a password toggle visibility to the password input fields in magento 2
checkout pages and I can't find a way to work with knockout js
.
I am using only javscript to add a span with a click event that changes the input field type from password to text, and it works on all inputs except the ones from the checkout pages.
I want to avoid making any changes to xml or template files, I was looking for a solution to do this only with javascript.
This is the code I have in my module:
require(['jquery'], function($) {$(function() {
var passwordInput = document.querySelectorAll('input[type="password"]');
passwordInput.forEach(function(selectedInput) {
var passwordToggle = document.createElement('span');
passwordToggle.setAttribute('class', 'show-password');
selectedInput.parentNode.appendChild(passwordToggle);
passwordToggle.addEventListener('click', function() {
if (selectedInput.type === "password") {
selectedInput.type = "text";
this.classList.add('visible');
} else {
selectedInput.type = "password";
this.classList.remove('visible');
}
});
});
});});