change type of input field with jQuery

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


        
相关标签:
29条回答
  • 2020-11-22 05:51

    Even easier... there's no need for all the dynamic element creation. Just create two separate fields, making one the 'real' password field (type="password") and one a 'fake' password field (type="text"), setting the text in the fake field to a light gray color and setting the initial value to 'Password'. Then add a few lines of Javascript with jQuery as below:

        <script type="text/javascript">
    
            function pwdFocus() {
                $('#fakepassword').hide();
                $('#password').show();
                $('#password').focus();
            }
    
            function pwdBlur() {
                if ($('#password').attr('value') == '') {
                    $('#password').hide();
                    $('#fakepassword').show();
                }
            }
        </script>
    
        <input style="color: #ccc" type="text" name="fakepassword" id="fakepassword" value="Password" onfocus="pwdFocus()" />
        <input style="display: none" type="password" name="password" id="password" value="" onblur="pwdBlur()" />
    

    So when the user enters the 'fake' password field it will be hidden, the real field will be shown, and the focus will move to the real field. They will never be able to enter text in the fake field.

    When the user leaves the real password field the script will see if it's empty, and if so will hide the real field and show the fake one.

    Be careful not to leave a space between the two input elements because IE will position one a little bit after the other (rendering the space) and the field will appear to move when the user enters/exits it.

    0 讨论(0)
  • 2020-11-22 05:52

    Try this
    Demo is here

    $(document).delegate('input[type="text"]','click', function() {
        $(this).replaceWith('<input type="password" value="'+this.value+'" id="'+this.id+'">');
    }); 
    $(document).delegate('input[type="password"]','click', function() {
        $(this).replaceWith('<input type="text" value="'+this.value+'" id="'+this.id+'">');
    }); 
    
    0 讨论(0)
  • 2020-11-22 05:54

    This works for me.

    $('#password').replaceWith($('#password').clone().attr('type', 'text'));
    
    0 讨论(0)
  • 2020-11-22 05:54

    I haven't tested in IE (since I needed this for an iPad site) - a form I couldn't change the HTML but I could add JS:

    document.getElementById('phonenumber').type = 'tel';
    

    (Old school JS is ugly next to all the jQuery!)

    But, http://bugs.jquery.com/ticket/1957 links to MSDN: "As of Microsoft Internet Explorer 5, the type property is read/write-once, but only when an input element is created with the createElement method and before it is added to the document." so maybe you could duplicate the element, change the type, add to DOM and remove the old one?

    0 讨论(0)
  • 2020-11-22 05:54

    I guess you could use a background-image that contains the word "password" and change it back to an empty background-image on .focus().

    .blur() ----> image with "password"

    .focus() -----> image with no "password"

    You could also do it with some CSS and jQuery. Have a text field show up exactly on top of the password field, hide() is on focus() and focus on the password field...

    0 讨论(0)
提交回复
热议问题