Toggle Password Visibility using Javascript
Submitted by nurhodelta_17 on Saturday, January 20, 2018 - 11:40.
This is the step by step procedure:
Step 1 : Add Dependency
In the header portion of your HTML, add the following:Step 2 : Add Style
Include the following style.- <style type="text/css">
- #myInput{
- font-size: 1em;
- padding: 1em;
- }
- </style>
Step 3 : Create the Input
Next, we create our input type.Step 4 : Adding our Script
Last step is to add our javascript code.- function showPass() {
- var icon = document.getElementById('icon');
- var txt = document.getElementById('btnText');
- var btn = document.getElementById('showBtn');
- var x = document.getElementById('myInput');
- if (x.type === 'password') {
- x.type = 'text';
- btn.className = 'btn btn-default';
- txt.innerHTML = 'Hide';
- icon.className = 'fa fa-eye-slash';
- } else {
- x.type = 'password';
- btn.className = 'btn btn-primary';
- txt.innerHTML = 'Show';
- icon.className = 'fa fa-eye';
- }
- }
Full HTML
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
- <style type="text/css">
- #myInput{
- font-size: 1em;
- padding: 1em;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-sm-4 col-sm-offset-4">
- </div>
- </div>
- </div>
- <script type="text/javascript">
- function showPass() {
- var icon = document.getElementById('icon');
- var txt = document.getElementById('btnText');
- var btn = document.getElementById('showBtn');
- var x = document.getElementById('myInput');
- if (x.type === 'password') {
- x.type = 'text';
- btn.className = 'btn btn-default';
- txt.innerHTML = 'Hide';
- icon.className = 'fa fa-eye-slash';
- } else {
- x.type = 'password';
- btn.className = 'btn btn-primary';
- txt.innerHTML = 'Show';
- icon.className = 'fa fa-eye';
- }
- }
- </script>
- </body>
- </html>
Add new comment
- 125 views