Toggle Password Visibility using jQuery
Submitted by nurhodelta_17 on Saturday, January 20, 2018 - 14:25.
This is the step by step procedure in this tutorial:
That ends this tutorial. Happy Coding :)
Add dependency
In the header portion of your HTML, add the following:Add style
Include the following style.- #myInput{
- font-size: 1em;
- padding: 1em;
- }
Add the Input
Next, we create our input type.Adding our jQuery Script
Last step is to add our jquery script.- $(document).ready(function(){
- $('#showBtn').click(function(){
- var type = $('#myInput').attr('type');
- if(type == 'password'){
- $('#myInput').attr('type', 'text');
- $('#btnText').text('Hide');
- $('#showBtn').removeClass('btn-primary').addClass('btn-default');
- $('#icon').removeClass('fa-eye').addClass('fa-eye-slash');
- }
- else{
- $('#myInput').attr('type', 'password');
- $('#btnText').text('Show');
- $('#showBtn').removeClass('btn-default').addClass('btn-primary');
- $('#icon').removeClass('fa-eye-slash').addClass('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">
- $(document).ready(function(){
- $('#showBtn').click(function(){
- var type = $('#myInput').attr('type');
- if(type == 'password'){
- $('#myInput').attr('type', 'text');
- $('#btnText').text('Hide');
- $('#showBtn').removeClass('btn-primary').addClass('btn-default');
- $('#icon').removeClass('fa-eye').addClass('fa-eye-slash');
- }
- else{
- $('#myInput').attr('type', 'password');
- $('#btnText').text('Show');
- $('#showBtn').removeClass('btn-default').addClass('btn-primary');
- $('#icon').removeClass('fa-eye-slash').addClass('fa-eye');
- }
- });
- });
- </script>
- </body>
- </html>
Add new comment
- 87 views