Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle form field type with jQuery

Tags:

jquery

Is there a way to add a checkbox or radio buttons on a page to toggle form fields between input type "text" and "password"?

The reason I put jQuery in subject line because I'm trying to use it exclusively and I have a lot of confidence in its potential. :)

Thanks.


UPDATE: I need to toggle because I need the user an ability to come back and review previously entered input. (Tip: This is not a login script.)

like image 737
santa Avatar asked Feb 16 '11 17:02

santa


1 Answers

I ended up simplifying this into the following solution:

$('input#checkbox').change(function(){
    var type    = ($(this).is(':checked') ? 'text' : 'password'), 
        input   = $('input#password'),
        replace = input.clone().attr('type', type)
        input.replaceWith(replace);
});
like image 59
Mark Garity Avatar answered Nov 15 '22 05:11

Mark Garity