单击图标后如何设置密码可见

时间:2019-02-05 05:55:27

标签: javascript html css font-awesome-5

我有三个输入字段,它们都是密码字段,我想要做的是在单击时在输入字段中放置fontawesome图标,以显示或隐藏密码

当前我有这个但有按钮,并且它仅对一个域而不是对所有三个域都起作用,我是否必须对这三个域调用3个不同的函数,或者可以用相同的函数来完成

代码段

function show() {
  var a = document.getElementById("confirm_password");
  var b = document.getElementById("display2");
  if (a.type == "password") {
    a.type = "text";

  } else {
    a.type = "password";

  }
}
<div class="col-lg-3">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show()" id="display"></button>
</div>
<div class="col-lg-3">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show()" id="display1"></button>

</div>
<div class="col-lg-3">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
  <button type="button" onclick="show()" id="display2"></button>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>

在我的代码段中,我有3个输入字段,我必须为所有输入字段提供一个图标,如果用户单击该图标,它将显示当前字段的密码

当前它仅适用于一个字段

4 个答案:

答案 0 :(得分:0)

function show(inputId) {
  var a = document.getElementById(inputId);
  if (a.type === "password") {
    a.type ="text";

  } else {
    a.type ="password";

  }
}
.show-button{
  position:absolute;
  right:22px;
  top: 32px;
  border:0px;
  background-color:transparent;
}
input{
width:100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="col-12 ">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <button class="show-button" type="button" onclick="show('currentPass')" id="display"><i class="fa fa-eye"></i></button>
</div>
<div class="col-12">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
  <button type="button" class="show-button" onclick="show('newPass')" id="display1"><i class="fa fa-eye"></i></button>

</div>
<div class="col-12">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
  <button class="show-button" type="button" onclick="show('confirm_password')" id="display2"><i class="fa fa-eye"></i></button>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>

在您的函数中传递输入的ID,您会很好。希望对您有所帮助。

答案 1 :(得分:0)

您可以使用removeAttribute删除type =“ password”,并使用setAttribute同时将类型设置为文本

function show(a) {
  var x=document.getElementById(a);
  var c=x.nextElementSibling
  if (x.getAttribute('type') == "password") {
  c.removeAttribute("class");
  c.setAttribute("class","fas fa-eye");
  x.removeAttribute("type");
    x.setAttribute("type","text");
  } else {
  x.removeAttribute("type");
    x.setAttribute('type','password');
 c.removeAttribute("class");
  c.setAttribute("class","fas fa-eye-slash");
  }
}
  
   
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<div class="col-lg-3">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <i onclick="show('currentPass')" class="fas fa-eye-slash" id="display"></i>
</div>
<div class="col-lg-3">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
 <i onclick="show('newPass')" class="fas fa-eye-slash" id="display"></i>

</div>
<div class="col-lg-3">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
 <i onclick="show('confirm_password')" class="fas fa-eye-slash" id="display"></i>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>

答案 2 :(得分:0)

如果要将其全部动态化,则需要在函数调用时传递文本框的ID,然后可以使用该ID动态执行逻辑。

function show(id) {
  var a = document.getElementById(id);
  if (a.type == "password") {
    a.type = "text";

  } else {
    a.type = "password";

  }
}
<div class="col-lg-3">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show('currentPass')" id="display"></button>
</div>
<div class="col-lg-3">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show('newPass')" id="display1"></button>

</div>
<div class="col-lg-3">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
  <button type="button" onclick="show('confirm_password')" id="display2"></button>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>

答案 3 :(得分:0)

您可以使用一个将inputElementId作为参数的函数来实现:

function show(inputElementId) {
  var a = document.getElementById(inputElementId);
  if (a.type == "password") {
    a.type = "text";

  } else {
    a.type = "password";

  }
}

您可以这样称呼它3次:

<div class="col-lg-3">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show(currentPass)" id="display"></button>
</div>
<div class="col-lg-3">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show(newPass)" id="display1"></button>

</div>
<div class="col-lg-3">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
  <button type="button" onclick="show(confirm_password)" id="display2"></button>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>