10

Possible Duplicate:
JQuery change type of input field

Demo here:

http://jsbin.com/welcome/73795/edit


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


<input type="password" name="password" />
<button>Show Password</button>

$(document).ready(function() {
  $('button').click(function() {
    $('input').attr('type', 'text');
  });
});

I'm trying to create a button (or checkbox) that users will be able to toggle to see their password written in plain text.

This is basically for portable devices where typing in passwords is a pain, and seeing it in plain text while writing is a UX improvement.

In my example above, changing the type of the input doesn't work.

Any suggestions on how to tackle this problem?

5
  • 2
    Try using two input elements with their contents in sync, and alternately hide one and show the other. Commented Jan 13, 2013 at 16:53
  • 3
    jQuery doesn't allow that. Check this : stackoverflow.com/questions/1544317/… Commented Jan 13, 2013 at 16:53
  • 2
    @TJ- Actually that's close enough to be a dupe. Commented Jan 13, 2013 at 16:54
  • 1
    Check this video: youtube.com/watch?v=5YeSqtB0kfE Commented Jul 8, 2014 at 13:37
  • 2017 - Changing the input type now works. I adapted a little bit the solution in Grijesh video: jsfiddle.net/herLkape. Tested with IE11, GC 59 and FF54. Commented Jul 6, 2017 at 15:44

1 Answer 1

16

You can't change type attribute.. See workaround here : http://jsfiddle.net/Ngtp7/2/

and a variation here : http://jsfiddle.net/Ngtp7/

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, your second example does just that: i.e. to change the type attribute.
Please take a better look, the script is creating a new element and deleting the previous one. Nice answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.