When a user clicks Show link, display the password

2019-01-14 21:50发布

I am trying to get this simple script to work. Basically, when a user clicks on the Show link, it will display the password in the password text box and hide it when it is clicked again. I have searched for solutions but couldn't find anything for what I need. Here is the code:

JavaScript

    function toggle_password(target){
    var tag = getElementById(target);
    var tag2 = getElementById("showhide");
    if (tag2.innerHTML == 'Show'){
        tag.setAttribute('type', 'text');   
        tag2.innerHTML = 'Hide';
    }
    else{
        tag.setAttribute('type', 'password');   
        tag2.innerHTML = 'Show';
    }

    }

HTML

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

When I click the link, nothing happens. I have tested this without using the if statement too and still did nothing.

10条回答
We Are One
2楼-- · 2019-01-14 22:30

Its really easy to achieve...I found this blog post which describes the way of doing it with a few lines of code. I think its not a good practice to keep an option like this because passwords must be treated seriously.

here is the code from that page; it uses the same approach as you described. but uses a check-box.and practically i have seen what he points out in oss' like linux. they don't even leave a trace of the password with an asterisk.(but the gui thing does, don't know they really care about pw privacy or they find it difficult to do it in command line ;-) )

var textbox_elem = document.getElementById("password");
    if(check_box.checked)
    textbox_elem.setAttribute("type", "text");
    else
    textbox_elem.setAttribute("type", "password");
查看更多
劫难
3楼-- · 2019-01-14 22:33

If we make a textbox type as "passowrd" then chars are not shown and but when we make a text-box type "text" chars are shown as we type.

Hence the logic is very simple. We can change the type of any HTML element by "setAttribute(type,newvalue)" function of JavaScript.

A complete code example can be seen on below link:

http://www.tutor4web.com/javascript/how-to-show-password-as-text-with-checkbox-using-javascript/

查看更多
做自己的国王
4楼-- · 2019-01-14 22:33

You could use inline script for a simple implementation of show/hide password.

   

 <form>
    <input 
    onmouseover="this.setAttribute('type','text')"               
    onmouseout="this.setAttribute('type','password')" 
    placeholder="Hover to show password!" 
    type="password" 
    name="password-login"
    id="password-login"
    >
    </form>   

查看更多
成全新的幸福
5楼-- · 2019-01-14 22:38

Don't change input type, change text size. Make it very small for Hide.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-14 22:44

Because of security reasons you can't change the type of an input element. You have to replace the entire element with a new one.

查看更多
Root(大扎)
7楼-- · 2019-01-14 22:50

http://www.voidtricks.com/password-show-hide-checkbox-click/ This is a simple tutorial to show a password in a input box when a checkbox is checked and hide it when the checkbox is unchecked using jQuery.

Javascript

<script type="text/javascript">
 $(document).ready(function () {
 $("#showHide").click(function () {
 if ($(".password").attr("type")=="password") {
 $(".password").attr("type", "text");
 }
 else{
 $(".password").attr("type", "password");
 }

 });
 });
</script>

<!-- language: lang-html -->

HTML *

<input type="password" class="password"><br>
    <input type="checkbox" id="showHide"> Show?

* We have a password input field with the class “password” and a checkbox with the id “showHide”

Easy solution for show / hide password using checkboxes, and this is noob level just copy/paste. DONE !!! ;-)

查看更多
登录 后发表回答