In a recent project at work, I had to add in functionality so that a user could see what they had typed in a password field when they click a link. One would think that it is as easy as using JavaScript to change the type attribute, but that of course doesn’t work in IE. Virtually all of the jQuery plugins that I found used a checkbox instead of a link to toggle between the two states. So, I set out to write my own.
If you just want to get to the code, you can download the plugin from GitHub or check out the demo.
This plugin works by adding a text field that takes the value of what is typed into the password field. Then, when the link is clicked the password field is hidden and the text field is shown.
The Markup
In the demo, we’ll just start with a simple form:
<form action="#">
<ol class="forms">
<li>
<label for="username">Username</label>
<input type="text" id="username" />
</li>
<li>
<label for="password">Password</label>
<input type="password" id="password" class="password" />
</li>
<li class="buttons">
<button type="submit">Submit</button>
</li>
</ol>
</form>
Add a Little Style
In the design that I was working on, the link to show the password actually sat on top of the password input field, so we just need to add a little bit of CSS to make that happen:
.forms li {
position: relative;
}
.show-password-link {
display: block;
position: absolute;
z-index: 11;
}
.password-showing {
position: absolute;
z-index: 10;
}
By default, the link to show the password has the class of show-password-link, but that can be changed in the plugin options. Also, the password-showing class is added to the text field that is used to show the password, but again, that can be changed via the plugin options.
And Now, JavaScript Takes Over
To get started, you will need to include jQuery and my plugin:
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.showPassword.min.js"></script>
Then, you can instantiate it like this:
$(document).ready(function() {$(':password').showPassword();});
Options
-
linkClass: 'show-password-link'
Class for the toggle link -
linkText: 'Show'
Text for the toggle link -
showPasswordLinkText: 'Hide'
Text for the toggle link when password is not masked -
showPasswordInputClass: 'password-showing'
Class for the text input that will show the password -
linkRightOffset: 0
Offset for the link from the right of parent -
linkTopOffset: 0
Offset for the link from the top of parent
So there you have it. Check out the demo and download the plugin from GitHub.
Categories
Recent Articles
May 2012
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 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 |
12 Comments
Doug Avery
08.09.2010i was wondering how you did this without changing the type on the input .... clever solution!
Mudit
08.10.2010its awesome man…really a good one… :-)
Manmohanjit Singh
08.12.2010Good job but when the password is shown, replace the text with ‘Hide’.
Just a suggestion.
Trevor
08.12.2010@Manmohanjit Singh-
That’s a good suggestion, I’m not sure why I didn’t do that from the start. I’ll definitely add that option.
Trevor
08.12.2010@Manmohanjit Singh-
I’ve made that option available in the plugin. The updated code is on GitHub.
Michael Grech
11.25.2010Awesome thanks man. I’ll have to post my example w/ the site goes live.
Thomas
12.22.2010Since the data is just given back from the text-input to the password-input on link click, this thing does not work in real life. Whom of the users is expected to know that he needs to switch back to hidden in order to make the password work?
Trevor
12.22.2010@Thomas-
That could easily be fixed by binding a submit handler to the form to update the password hidden field if the visible password field is showing.
Thomas
12.22.2010@Trevor-
Thank you for the fast response. You’re right, but in my case there are several submit-handlers running (validation and case-dependent AJAX form submission instead of real ones). I guess I will sync the passwords at every change and keystroke.
I just expected it to work out-of-the-box, as many people will do who find this plugin. (And do not read carefully - as I did.)
Regards.
Trevor
12.22.2010@Thomas-
Just pushed a fix that addresses that problem.
Neokio
03.12.2011How about a setting for “begin with password showing?”
Trevor
03.12.2011@Neokio-
I don’t think that it something that I would build into the plugin, but you could easily make that happen by triggering a click on the show password link.
Too late, comments are closed!
Don’t worry, you can email me or contact me on Twitter.