change type of input field with jQuery

后端 未结 29 2625
青春惊慌失措
青春惊慌失措 2020-11-22 05:13
$(document).ready(function() {
    // #login-box password field
    $(\'#password\').attr(\'type\', \'text\');
    $(\'#passwo         


        
29条回答
  •  天涯浪人
    2020-11-22 05:45

    I received the same error message while attempting to do this in Firefox 5.

    I solved it using the code below:

    
    

    And to use it, just set the onFocus and onBlur attributes of your fields to something like the following:

    
    
    
    

    I use this for a username field as well, so it toggles a default value. Just set the second parameter of the function to '' when you call it.

    Also it might be worth noting that the default type of my password field is actually password, just in case a user doesn't have javascript enabled or if something goes wrong, that way their password is still protected.

    The $(document).ready function is jQuery, and loads when the document has finished loading. This then changes the password field to a text field. Obviously you'll have to change 'password_field_id' to your password field's id.

    Feel free to use and modify the code!

    Hope this helps everyone who had the same problem I did :)

    -- CJ Kent

    EDIT: Good solution but not absolute. Works on on FF8 and IE8 BUT not fully on Chrome(16.0.912.75 ver). Chrome does not display the Password text when the page loads. Also - FF will display your password when autofill is switched on.

提交回复
热议问题