change type of input field with jQuery

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


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

    It works much easier with that:

    document.querySelector('input[type=password]').setAttribute('type', 'text');
    

    and in order to turn it back to password field again,(assuming the password field is the 2nd input tag with text type):

    document.querySelectorAll('input[type=text]')[1].setAttribute('type', 'password')
    
    0 讨论(0)
  • 2020-11-22 05:44

    A more cross-browser solution… I hope the gist of this helps someone out there.

    This solution tries to set the type attribute, and if it fails, it simply creates a new <input> element, preserving element attributes and event handlers.

    changeTypeAttr.js (GitHub Gist):

    /* x is the <input/> element
       type is the type you want to change it to.
       jQuery is required and assumed to be the "$" variable */
    function changeType(x, type) {
        x = $(x);
        if(x.prop('type') == type)
            return x; //That was easy.
        try {
            return x.prop('type', type); //Stupid IE security will not allow this
        } catch(e) {
            //Try re-creating the element (yep... this sucks)
            //jQuery has no html() method for the element, so we have to put into a div first
            var html = $("<div>").append(x.clone()).html();
            var regex = /type=(\")?([^\"\s]+)(\")?/; //matches type=text or type="text"
            //If no match, we add the type attribute to the end; otherwise, we replace
            var tmp = $(html.match(regex) == null ?
                html.replace(">", ' type="' + type + '">') :
                html.replace(regex, 'type="' + type + '"') );
            //Copy data from old element
            tmp.data('type', x.data('type') );
            var events = x.data('events');
            var cb = function(events) {
                return function() {
                    //Bind all prior events
                    for(i in events)
                    {
                        var y = events[i];
                        for(j in y)
                            tmp.bind(i, y[j].handler);
                    }
                }
            }(events);
            x.replaceWith(tmp);
            setTimeout(cb, 10); //Wait a bit to call function
            return tmp;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:44

    I just did the following to change the type of an input:

    $('#ID_of_element')[0].type = 'text';
    

    and it works.

    I was needing to do this because I was using jQuery UI datepickers' in an ASP NET Core 3.1 project and they were not working properly on Chromium-based browsers (see: https://stackoverflow.com/a/61296225/7420301).

    0 讨论(0)
  • 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:

    <script type="text/javascript" language="JavaScript">
    
    $(document).ready(function()
    {
        var passfield = document.getElementById('password_field_id');
        passfield.type = 'text';
    });
    
    function focusCheckDefaultValue(field, type, defaultValue)
    {
        if (field.value == defaultValue)
        {
            field.value = '';
        }
        if (type == 'pass')
        {
            field.type = 'password';
        }
    }
    function blurCheckDefaultValue(field, type, defaultValue)
    {
        if (field.value == '')
        {
            field.value = defaultValue;
        }
        if (type == 'pass' && field.value == defaultValue)
        {
            field.type = 'text';
        }
        else if (type == 'pass' && field.value != defaultValue)
        {
            field.type = 'password';
        }
    }
    
    </script>
    

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

    <input type="text" value="Username" name="username" id="username" 
        onFocus="javascript:focusCheckDefaultValue(this, '', 'Username -OR- Email Address');"
        onBlur="javascript:blurCheckDefaultValue(this, '', 'Username -OR- Email Address');">
    
    <input type="password" value="Password" name="pass" id="pass"
        onFocus="javascript:focusCheckDefaultValue(this, 'pass', 'Password');"
        onBlur="javascript:blurCheckDefaultValue(this, 'pass', 'Password');">
    

    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.

    0 讨论(0)
  • 2020-11-22 05:45
    $('#pass').focus(function() { 
    $('#pass').replaceWith("<input id='password' size='70' type='password' value='' name='password'>");
    $('#password').focus();
    });
    
    <input id='pass' size='70' type='text' value='password' name='password'>
    
    0 讨论(0)
  • 2020-11-22 05:45

    Simply this:

    this.type = 'password';
    

    such as

    $("#password").click(function(){
        this.type = 'password';
    });
    

    This is assuming that your input field was set to "text" before hand.

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