Show/hide password onClick of button using Javascript only

后端 未结 13 907
孤城傲影
孤城傲影 2020-12-08 22:47

I want to create password toggle function when clicked on the eye icon using Javascript only. I have written code for it but it works only to show the password text and not

相关标签:
13条回答
  • 2020-12-08 23:01

    Follow these steps: Download the images given below. Make a folder. Add your html file and the images in the same folder. Replace the value of "b.src" in javascript as well as in your html code accordingly.

    Images :

    function show() {
      var a = document.getElementById("pwd");
      var b = document.getElementById("EYE");
      if (a.type == "password") {
        a.type = "text";
        b.src = "https://i.stack.imgur.com/waw4z.png";
      } else {
        a.type = "password";
        b.src = "https://i.stack.imgur.com/Oyk1g.png";
      }
    }
    <input type="password" id="pwd">
    <button onclick="show()"><img src="https://i.stack.imgur.com/Oyk1g.png" id="EYE"></button>

    0 讨论(0)
  • 2020-12-08 23:07

    The easiest way is using a button with an onclick attribute that toggles the type of the input.

    <input type="password" id="password" value="myPassword"/>
    <button onclick="if (password.type == 'text') password.type = 'password';
      else password.type = 'text';">toggle</button>

    0 讨论(0)
  • 2020-12-08 23:09

    You don't need to maintain one extra "pwShown" variable to decide whether to show text or hide it. All you need to do is to examine "type" attribute of "pwd" element as below :

    Working Example

    JavaScript :

    document.getElementById("eye").addEventListener("click", function(e){
            var pwd = document.getElementById("pwd");
            if(pwd.getAttribute("type")=="password"){
                pwd.setAttribute("type","text");
            } else {
                pwd.setAttribute("type","password");
            }
        });
    

    HTML :

    <input type="password" placeholder="Password" id="pwd" class="masked" name="password" />
            <button type="button" id="eye">
                <img src="eye.png" alt="eye"/>
             </button>
    
    0 讨论(0)
  • 2020-12-08 23:10

    The variable "pwShown" is misspelled (as "pwShow") in the else section of your Javascript code. Therefore, pwShown never gets reset to 0.

    0 讨论(0)
  • 2020-12-08 23:11
    function myFunction() {
        var x = document.getElementById("myInput");
        if (x.type === "password") {
            x.type = "text";
        } else {
            x.type = "password";
        }
    } 
    

    see also https://www.w3schools.com/howto/howto_js_toggle_password.asp

    0 讨论(0)
  • 2020-12-08 23:13

    Dump the eye image and instead use a button showing "Show" or "Hide" according to its state. Then you just click on the text of the button. You can stylize the button to be borderless and initially with the same background as its surrounds. Highlight the button by setting .show:hover to either brighten the background of the button or else to brighten the color of the Show/Hide text. By putting the input and button into the same span, you will have them both inline ( CSS - span{display: inline-block;} ) and vertically align off the same bottom.

    Use an ordinary text input just below the span for space to display the validation error alerts. Make sure its tab index is -1 and its background color & border is the same as its surrounding.

      .
      .
      .
    
      <span class="pwSpan">
        <input type="password" placeholder="Password" id="pwd"  class="masked"  name="password" onblur="return checkPassword();"/>
        <button type="button" class="show" id="show" value="Show" onclick="showHide()" tabIndex="-1" autocomplete="off" >
        </button>
      </span>
      <input type="text" class="error" name="pwErr" value="" tabIndex="-1" />
      .
      .
      .
    
    function showHide()
    {
       const pwField = document.getElementById("pwd");
       const showHideValue = document.getElementById("show").value;
    
       if(showHideValue.trim() === "Show")
       {
          showHideValue = "Hide";
          pwField.setAttribute('type', 'text');
       }
       else 
       {
          showHideValue = "Show";
          pwField.setAttribute('type', 'password');
       }
    }
    
    0 讨论(0)
提交回复
热议问题