change type of input field with jQuery

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


        
相关标签:
29条回答
  • 2020-11-22 05:46
    jQuery.fn.outerHTML = function() {
        return $(this).clone().wrap('<div>').parent().html();
    };
    $('input#password').replaceWith($('input.password').outerHTML().replace(/text/g,'password'));
    
    0 讨论(0)
  • 2020-11-22 05:46

    Just another option for all the IE8 lovers, and it works perfect in newer browsers. You can just color the text to match the background of the input. If you have a single field, this will change the color to black when you click/focus on the field. I would not use this on a public site since it would 'confuse' most people, but I am using it in an ADMIN section where only one person has access to the users passwords.

    $('#MyPass').click(function() {
        $(this).css('color', '#000000');
    });
    

    -OR-

    $('#MyPass').focus(function() {
        $(this).css('color', '#000000');
    });
    

    This, also needed, will change the text back to white when you leave the field. Simple, simple, simple.

    $("#MyPass").blur(function() {
        $(this).css('color', '#ffffff');
    });
    

    [ Another Option ] Now, if you have several fields that you are checking for, all with the same ID, as I am using it for, add a class of 'pass' to the fields you want to hide the text in. Set the password fields type to 'text'. This way, only the fields with a class of 'pass' will be changed.

    <input type="text" class="pass" id="inp_2" value="snoogle"/>
    
    $('[id^=inp_]').click(function() {
        if ($(this).hasClass("pass")) {
            $(this).css('color', '#000000');
        }
        // rest of code
    });
    

    Here is the second part of this. This changes the text back to white after you leave the field.

    $("[id^=inp_]").blur(function() {
        if ($(this).hasClass("pass")) {
            $(this).css('color', '#ffffff');
        }
        // rest of code
    });
    
    0 讨论(0)
  • 2020-11-22 05:47

    Just create a new field to bypass this security thing:

    var $oldPassword = $("#password");
    var $newPassword = $("<input type='text' />")
                              .val($oldPassword.val())
                              .appendTo($oldPassword.parent());
    $oldPassword.remove();
    $newPassword.attr('id','password');
    
    0 讨论(0)
  • 2020-11-22 05:48

    An ultimate way to use jQuery:


    Leave the original input field hidden from the screen.

    $("#Password").hide(); //Hide it first
    var old_id = $("#Password").attr("id"); //Store ID of hidden input for later use
    $("#Password").attr("id","Password_hidden"); //Change ID for hidden input
    

    Create new input field on the fly by JavaScript.

    var new_input = document.createElement("input");
    

    Migrate the ID and value from hidden input field to the new input field.

    new_input.setAttribute("id", old_id); //Assign old hidden input ID to new input
    new_input.setAttribute("type","text"); //Set proper type
    new_input.value = $("#Password_hidden").val(); //Transfer the value to new input
    $("#Password_hidden").after(new_input); //Add new input right behind the hidden input
    

    To get around the error on IE like type property cannot be changed, you may find this useful as belows:

    Attach click/focus/change event to new input element, in order to trigger the same event on hidden input.

    $(new_input).click(function(){$("#Password_hidden").click();});
    //Replicate above line for all other events like focus, change and so on...
    

    Old hidden input element is still inside the DOM so will react with the event triggered by new input element. As ID is swapped, new input element will act like the old one and respond to any function call to old hidden input's ID, but looks different.

    A little bit tricky but WORKS!!! ;-)

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

    use this one it is very easy

    <input id="pw" onclick="document.getElementById('pw').type='password';
      document.getElementById('pw').value='';"
      name="password" type="text" value="Password" />
    
    0 讨论(0)
  • 2020-11-22 05:50

    Here is a method which uses an image next to the password field to toggle between seeing the password (text input) and not seeing it (password input). I use an "open eye" and "closed eye" image, but you can use whatever suits you. The way it works is having two inputs/images and upon clicking the image, the value is copied from the visible input to the hidden one, and then their visibility is swapped. Unlike many of the other answers which use hardcoded names, this one is general enough to use it multiple times on a page. It also degrades gracefully if JavaScript is unavailable.

    Here is what two of these look like on a page. In this example, the Password-A has been revealed by clicking on its eye.

    $(document).ready(function() {
      $('img.eye').show();
      $('span.pnt').on('click', 'img', function() {
        var self = $(this);
        var myinp = self.prev();
        var myspan = self.parent();
        var mypnt = myspan.parent();
        var otspan = mypnt.children().not(myspan);
        var otinp = otspan.children().first();
        otinp.val(myinp.val());
        myspan.hide();
        otspan.show();
      });
    });
    img.eye {
      vertical-align: middle;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <form>
    <b>Password-A:</b>
    <span class="pnt">
    <span>
    <input type="password" name="passa">
    <img src="eye-open.png" class="eye" alt="O" style="display:none">
    </span>
    <span style="display:none">
    <input type="text">
    <img src="eye-closed.png" class="eye" alt="*">
    </span>
    </span>
    </form>
    
    <form>
    <b>Password-B:</b>
    <span class="pnt">
    <span>             
    <input type="password" name="passb">
    <img src="eye-open.png" class="eye" alt="O" style="display:none">
    </span> 
    <span style="display:none">            
    <input type="text">
    <img src="eye-closed.png" class="eye" alt="*">
    </span> 
    </span>
    </form>

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