简体   繁体   中英

How to change/toggle the icon inside button?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>
<style>
#togglePassword{
    margin-left: -5px;
    margin-top: 11px;
    cursor: pointer;
}

</style>

</head>

<body>
    <p:inputText id="productadminpassword" 
            value="password" 
            type="password">
      </p:inputText>
 
    <button type="button" onclick="showHide();" >
        
        <i class='fa fa-eye-slash' id="eye"  />
    </button>
 
 <script type="text/javascript">
  function showHide() {
     
      if (productadminpassword.type =="password") {
          productadminpassword.type = 'text';
         
      }
    
    else {
        productadminpassword.type = 'password';
        
        
    }
  };
</script>
</body>
</html>

This is what i have done.All i need is to change the eye-slash icon to eye inside button along with change of types(text to password and vice-versa).This code is changing the types but not icon.I dont know how to do it.If anyone knows,Kindly help me out.

在此处输入图像描述

在此处输入图像描述

You can use Element.classList to add or remove classes from DOM

function showHide(obj) {
    var productadminpassword = document.getElementById('productadminpassword');
    var i = document.getElementById('eye')
    if (productadminpassword.type =="password") {
        productadminpassword.type = 'text';
        i.classList.remove("fa-eye-slash");
        i.classList.add("fa-eye");
    } else {
        productadminpassword.type = 'password';
        i.classList.remove("fa-eye");
        i.classList.add("fa-eye-slash");
    }
};

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