views:

62

answers:

1

Trying to change input type attribute from password to text.

$('.form').find('input:password').attr({type:"text"});

Why this doesn't work?

+4  A: 

You can't do this with jQuery, it explicitly forbids it because IE doesn't support it (check your console you'll see an error.

You have to remove the input and create a new one if that's what you're after, for example:

$('.form').find('input:password').each(function() {
   $("<input type='text' />").attr({ name: this.name, value: this.value }).insertBefore(this);
}).remove();

You can give it a try here

To be clear on the restriction, jQuery will not allow changing type on a <button> or <input> so the behavior is cross-browser consistent (since IE doens't allow it, they decided it's disallowed everywhere). When trying you'll get this error in the console:

Error: type property can't be changed

Nick Craver
damn, lovely IE
Happy
bah, beat me to it :-)
Andy E
@Andy - Doesn't it get confusing with your body here and your head on meta? :)
Nick Craver
@Nick, I'm too scared to edit your code, but I'm thinking you've got a typo: `.inserBefore(this)`
David Thomas
@ricebowl - You can edit away whenever you see an error, or post a more correct answer :) I appreciate the comment, fixed!
Nick Craver
@Nick, yes :) it's especially difficult reading questions without a head. Fortunately, there's always the jQuery braille plugin ;)
Andy E
Oh, I wasn't scared of *you*, I was scared that you might know of an arcane, and mystical, jQuery function of which I was unaware and knew not the power of. I just didn't want to embarrass myself by wrongly stating that `inserBefore` didn't exist... =)
David Thomas
this solution doesn't work
Happy
@WorkingHard - You're right the document fragment creation doesn't work there, seems it should...I'll file a bug with jQuery core later today, for now change it to use `.attr()` like the updated answer, I added a working demo here: http://jsfiddle.net/nick_craver/dHzzN/
Nick Craver
A bit unfortunate that jQuery decided to block this ability for all browsers just because IE doesn't support it.
scunliffe