0

I have an input type password that I want to change to text when an immage is clicked.

This is what I did:

$('#txtGenerate').attr("type", "text");

But it's not working.. What can I do?

RononDex
  • 3,876
  • 19
  • 35
user3226885
  • 25
  • 1
  • 2
  • Some browsers don't really support changing the type of an `input` element. You should consider replacing the password input with a text input instead. From the [`.attr` documentation](http://api.jquery.com/attr/) (which you read, right?): *"**Note:** Attempting to change the `type` attribute (or property) of an `input` element created via HTML or already in an HTML document will result in an error being thrown by Internet Explorer 6, 7, or 8."* – Felix Kling Jan 23 '14 at 08:36
  • Works Fine [here](http://jsfiddle.net/Lm72s/) – Dhaval Marthak Jan 23 '14 at 08:38

3 Answers3

3

You can do like this:

$('#txtGenerate').clone().prop('type','text').insertAfter('#txtGenerate').prev().remove();
  1. Clone the current input

  2. Change the input type

  3. Insert cloned input after old input

  4. Remove old input

Demo

Felix
  • 36,929
  • 7
  • 39
  • 54
2

try this:

 $('#txtGenerate').prop('type', 'text');
Pesulap
  • 824
  • 8
  • 19
1

Use prop instead:

$('#txtGenerate').removeAttr("type");
$('#txtGenerate').prop('type', 'password');
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114