简体   繁体   中英

How to remove and add input type with jQuery

Hello Im trying to create a login, everything its ok but in my password input I want a span that when user click on it show the password and when click again hide the password at the moment I only have the first step (show the password).

Here is the code

You should try with this:

$('#xd').on('mouseup', function () {
    $('#Contraseña').attr('type', 'password')
});

$('#xd').on('mousedown', function () {
    $('#Contraseña').attr('type', 'text')
});

Code here

Use this code

 $("button").click(function(){ if ($("input").attr("type") == "password"){ $("input").attr("type", "text"); $(this).text("Hide password"); } else { $("input").attr("type", "password"); $(this).text("Show password"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="password" /> <button>Show password</button> 

You can just create a variable & initially assign it to false , Now on click change this variable to true and toggle the attr type

var shown=false;
    $('#xd').click(function () {
      shown=!shown;
        $('#Contraseña').attr('type', shown ? 'text' : 'password')
    });

Try this ;)

<label for="pass_field_id">Password:</label>
<input type="password" value="your_passowrd" name="password" id="pass_field_id" />
<a href="#" onclick="show_hide_password('pass_field_id');" id="showhide">Show</a>

<script>
function swapInput(tag, type) {
  var el = document.createElement('input');
  el.id = tag.id;
  el.type = type;
  el.name = tag.name;
  el.value = tag.value;
  tag.parentNode.insertBefore(el, tag);
  tag.parentNode.removeChild(tag);
}

function show_hide_password(target){
    var d = document;
    var tag = d.getElementById(target);
    var tag2 = d.getElementById("showhide");

    if (tag2.innerHTML == 'Show'){

        swapInput(tag, 'text');
        tag2.innerHTML = 'Hide';

    } else {
        swapInput(tag, 'password');
        tag2.innerHTML = 'Show';
    }
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM