3

I want when I Start typing in textbox then the Forgot text replace it to View to user can view what password he typing and if password textbox is blank then it show default text as Forgot. If show View and use click View link then show text in textbox or View replace in Hide and if click Hide then textbox text become password format.I am not familiar with jquery please any wanna help us how I make it.

My code is below:

 <div class="input_field">
                        <span><i class="fa fa-lock" aria-hidden="true"></i></span>
                       <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password"/><a class="hyperlink link" href="forgot.com" id="link" style="float: right; margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="I forgotten my password" >Forgot?</a></div>

2 Answers2

2

If I don't misunderstood your requirements then this way you can try with keyup event,

let link = $('#link');
let password = $('#password');
password.keyup(function() {
  if (!!this.value) {
    $('#link').text('View');
  } else {
    $('#link').text('Forgot?');
  }

  link.click(function(e) {
    e.preventDefault();
    if (this.text === "View") {
      $("#password").attr('type', 'text');
      $('#link').text('Hide');
    } else {
      $("#password").attr('type', 'password');
      $('#link').text('View');
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input_field">
  <span><i class="fa fa-lock" aria-hidden="true"></i></span>
  <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password" /><a class="hyperlink link" href="forgot.com" id="link" style="float: right; margin-right: 5px;position: absolute;top: 7px;right: 0px;"
    title="I forgotten my password">Forgot?</a></div>
Always Sunny
  • 32,751
  • 7
  • 52
  • 86
  • thanks but still there is issue that is (1) When I click `view` button then password not show in texts and `view` not replace into `hide`. – Pritam Gosai Sep 22 '18 at 14:18
  • @PritamGosai then could you plz edit your question again with full requirements because I didn't find anywhere on your question about `hide` – Always Sunny Sep 22 '18 at 14:43
  • @PritamGosai I've **Edited** my answer for your. Have a look again – Always Sunny Sep 22 '18 at 15:12
  • 1
    @PritamGosai Let me know it is working or not as per your requirements? – Always Sunny Sep 22 '18 at 15:20
  • @PritamGosai ha ha. I'm a gentle guy bro as per I know myself..it's just a name from bollywood movie dialogue :). Let me try my best I can answer your new question or not – Always Sunny Oct 01 '18 at 15:30
2

Change your html code similar code below. (add another anchor to toggle password visibility, and add data-l attribute to the anchorss for using in js code):

window.onload=function(){
   var links=document.querySelectorAll("[data-l]");
   var pass=document.querySelector("#password");
   pass.addEventListener("keyup", function(){
        links.forEach(function(l){
            l.style.display= !!pass.value.trim().length==(l.getAttribute("data-l")=="true")?"":"none";
         })
   });
   document.querySelector("[data-l='true']").addEventListener("click", function(){
      pass.type=pass.type=="password"?"text":"password";
      this.innerHTML = pass.type=="text" ? "Hide Password" : "Show Password";
   });
};
<div class="input_field">
  <span>
         <i class="fa fa-lock" aria-hidden="true"></i>
  </span>
  <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password"/>
   <a class="hyperlink link" data-l="false" href="forgot.com" id="link" style=" margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="I forgotten my password" >Forgot?</a>
   <a class="hyperlink link" data-l="true" href="javascript:;" id="link" style="display:none;margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="toggle password" >Show Password</a>
</div>

New part: If you want to show eye icon instead of anchor's text, you can use code below:

window.onload=function(){
    var links=document.querySelectorAll("[data-l]");
   var pass=document.querySelector("#password");
   pass.addEventListener("keyup", function(){
        links.forEach(function(l){
             l.style.display= !!pass.value.trim().length==(l.getAttribute("data-l")=="true")?"":"none";
         })
   });
   document.querySelector("[data-l='true']").addEventListener("click", function(){
       pass.type=pass.type=="password"?"text":"password";
       this.setAttribute("data-tp", pass.type=="text" ? "hide" : "show");
   });
};
[data-tp='hide']{
    display: inline-block;
    width: 25px;
    height: 25px;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAPRSURBVGhD7ZpNqFVVGIZvZpaammkUERiikGZY/kwjoShKGzVI0YkOq5GgDQp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AZhInXimIhOnyUzBYE4SrwQ8gVs2ayTcBTfm3Q8dkplkBrdYQJSzNczOlveaql+YllftYltYfgRsIMagSPqkYqJ8NPva5bGaSTMV3gHXTUkgbdMtgPpgQnOy5qswMSbbqtLrKjEH3zFyJ6pm5UtXVZvwTuuv7RiqZ0YT/1NBoaaaxT6KnBqqv7xy8PjUHNT8LeQAAAABJRU5ErkJggg==);
    background-size: 100%;
}
[data-tp='show']{
    display: inline-block;
    width: 25px;
    height: 25px;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAANqSURBVGhD7dhNqJVFHMfxqxm28SUqU1fqwjIKjMR2qSEilIGSCKYupI22DAlKs8TMN8gSbVNqRhSGEK60dlGBC0FcCEIoki8LFYM0S836fuGM/J07zzme682rMD/4wLkz88zM89x55nnO6ampqampqampqbmvMgxT8BrewFstfrbsOdjmnssDmImPcQQ38G8H/+AwtuBF2MeAZRTW4SxKk+2GfayFfd61TMB2XEFpUrqMQziAb1r2t8qsKx2jP7EN4/G/ZQS24hryCfyN77EcT6PdUrFuEmzryf2FvD/HcKkOR79mPk4jH9Ayb+TH0Nd47Ao09f8q7jgjsRf5AMkMtMtDcAeTn9tlFkpjyDk4lz7FwY+j1LFc701x+byHP5Da+3k12i07d704RuRcnFNXeR1XETs6iKPh77fRlN2Ix0Y70JR3kNo5lmPGY72flqJjBmEN4sE+Fz7AEJxplWk6SpmGeHzJCyjFZ0pq4/3xINzi82eT/23nWowVnyIe4HKYC2N9rBuHUtzZYrsSd6RS3NpjuzTZeYjLVG7TvU7Ggk8QG56DrxMpboWx/lGU4jMjtiuxTSn2GdvFVxnn4pxivXO+5WQ+RGzwG55EnvgMKdWbTYh9lWxAKT5fUhvHymO9c4t9OfebyU/kFJ5CnrjXz7agkGfR7n3L96zJKMU+UzvnkMc5WR77u+VE/Pfka/s8nkfMPqT69RY0xPem2Ff0PpqyGanddxaEOJcLiH31WlrGAm+g2NAb7GWkvIlUdwyD0ZQlsE1svxhNsa9fkdo7VsocXEKqkxe+10nEvIu4NNL264NsLK4j1S1ApzzS0ikLkfp1jDFwzHz79fNK3Fa8mvnb7Y+YiD2hzCvYHy92vpCeQOrXMZ7AT6FMvjkvQlfxhsxfUTy5XYhXyEHb/os7xGO/RerPvnciv5BetKZNomMexteIHZZ8AZ/83cZjvkSpz+gr9PmlMeYV5Pt3zmXgcrjd+BzKl07OMV9Cv8Z7YSPafcNzOXyGqSgtN8us+xylL1SJY/jQ7PcvVjGj4Y8Gfi0tTSK5iB/g0pSff0epbWKfH+Fx3LW4PbpVn0RpUt1wx1oF+xyw+CDzqeurwi9ot2QS2/wMj2lahgOeoXgGbhDLkH6g87Nl/jhhm5qampqampqamvsiPT3/AVfe3H38IFoKAAAAAElFTkSuQmCC);
    background-size: 100%;
}
<div class="input_field">
  <span>
         <i class="fa fa-lock" aria-hidden="true"></i>
  </span>
  <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password"/>
   <a class="hyperlink link" data-l="false" href="forgot.com" id="link" style=" margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="I forgotten my password" >Forgot?</a>
   <a class="hyperlink link" data-l="true" href="javascript:;" id="link" style="display:none;margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="toggle password" data-tp='show'></a>
</div>
Hassan Sadeghi
  • 1,296
  • 2
  • 7
  • 14
  • Why toggle link not show `Hide` when show password clicking on Toggle password ? – Pritam Gosai Sep 22 '18 at 15:29
  • 1
    I've updated my answer. now clicking on it, changes the `anchor`'s text :) – Hassan Sadeghi Sep 22 '18 at 18:28
  • I thumbs up your answer , it is possible to add eye icon instead show and Hide text in Jquery? – Pritam Gosai Sep 23 '18 at 02:05
  • 1
    I've added a new part to my answer to showing how to do it; please look at it, and upvote my answer if that was useful :) – Hassan Sadeghi Sep 23 '18 at 05:41
  • This question `is possible to add eye icon instead show and Hide text in Jquery?` was added by you in a comment and was not a part of your main question while my answer was for your original question. removing the accept mark was not pleasant but i will update my answer :). hope it can help you... (In addition, my added part in my answer was totality of what you must do; A answer in response of a comment.). **I Have updated it now.** – Hassan Sadeghi Oct 01 '18 at 09:23
  • Thanks but, still it is url base 64. Could you tell me what is the benifit of url and base 64.? For my knowledge – Pritam Gosai Oct 01 '18 at 10:04
  • `Base64` will assigns real content (data) of an image to it and will load completely with your html (if you put these `css` in your `html` content). this causes that our images will be exist when user needs them (typing in our input box). `simple urls` needs loading image from an external resource but this not. in this way the images will load immediately. (if i understand your problem correctly) – Hassan Sadeghi Oct 01 '18 at 10:31
  • Yes,thank u for help `https://stackoverflow.com/questions/52587928/popup-when-click-link-little-like-stackoverflow-inbox-in-css` – Pritam Gosai Oct 01 '18 at 10:44
  • Please delte your above comment @Hassan and respond to that answer if you have – Pritam Gosai Oct 01 '18 at 15:24
  • My free times is very low @Pritam Gosai, but I will read your question as soon as possible :) – Hassan Sadeghi Oct 01 '18 at 16:14