Site icon Puneet Sharma – Freelance Web Developer

Generic Javascript code for hide-show input-password

// hide show password
if ( document.querySelectorAll('.toggle-password') ){
    let hideshowpassbtns = document.querySelectorAll('.toggle-password');
    hideshowpassbtns.forEach(item => {
        item.addEventListener('click', e => {
            let toggleId = item.getAttribute('toggle');
            let input = document.querySelector(toggleId);
            if( input.getAttribute("type") == "password" ){
                input.setAttribute("type", "text");
                item.classList.add('active');
            }else{
                input.setAttribute("type", "password");
                item.classList.remove('active');
            }
            console.log(input);
        })
    });
}
Exit mobile version