Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change input type

Trying to change input type attribute from password to text.

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

Why this doesn't work?

like image 686
James Avatar asked Aug 22 '10 12:08

James


People also ask

How to change input field type with jQuery?

$(document). ready(function() { // #login-box password field $('#password'). attr('type', 'text'); $('#password'). val('Password'); });

How to call change function in jQuery?

jQuery change() MethodThe change() method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected.

How do you change the input type in HTML?

To change the type of input it is (button to text, for example), use: myButton. type = "text"; //changes the input type from 'button' to 'text'. Another way to change or create an attribute is to use a method like element.


2 Answers

my solution:

    $('#myinput').clone().attr('type','tel').insertAfter('#myinput').prev().remove(); 
like image 183
Alon Avatar answered Oct 09 '22 09:10

Alon


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

like image 23
Nick Craver Avatar answered Oct 09 '22 09:10

Nick Craver