search
HomeWeb Front-endHTML TutorialHow to hide and display passwords in html

htmlHow to realize password hiding and display: first write the HTML interface tag and css style; then directly modify "type=text" and "type=password" to display and hide the password input box.

How to hide and display passwords in html

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

htmlHow to hide and display passwords?

Implementation of text display and hiding of password input box in HTML5 form

Problem description and ideas

The password type of password input box in HTML5 form can be Hide the content entered by the user, but sometimes it is used to allow the user to freely display or hide the content of the input box: to implement this function, the first thing that comes to mind is to use js to dynamically change the type of input, that is, type = password to type = text hidden The password will be displayed, but in fact it cannot be realized and has no effect, so I can only change my mind:

[Update: The latest type setting has taken effect, you can directly modify type=text and type=password To display and hide the password input box, the updated code is shown below. Please abandon the original method]

Put two inputs, one is password and the other is text, to jointly monitor the user's input event. Each time the user For this switch, we use js to control the display and hiding of the two inputs to achieve this effect.

How to hide and display passwords in html

Implementation steps:

First write the HTML interface tags and css style (the text input is hidden first: style ="display:none", subsequent display and hiding operations are also implemented by changing the display attribute)

CSS:

<style type="text/css">
body{
margin:0px;  
background-color: white; 
font-family: &#39;PT Sans&#39;, Helvetica, Arial, sans-serif;  
text-align: center;  
color: #A6A6A6;  
}
/*输入框样式,去掉背景阴影模仿原生应用的输入框*/
input{
width: 100%;  
height: 50px;  
border:none;  
padding-left:3px;  
font-size: 18px;  
}
input:focus {  
    outline: none;  
}
/*显示隐藏密码图片*/
img{
width: 40px;
height: 25px;
position: absolute;  
right: 0px; 
margin: 15px;  
}
/*登录按钮*/
button{  
    width: 200px;  
    height: 50px;  
    margin-top: 25px;  
    background: #1E90FF;  
    border-radius: 10px;  
    border:none;
    font-size: 18px;
    font-weight: 700;  
    color: #fff;
}
button:hover {
background: #79A84B;  
outline: 0;
}
/*输入框底部半透明横线*/
.input_block {
border-bottom: 1px solid rgba(0,0,0,.1);
}
/*container*/
#page_container{
margin: 50px;
}
</style>

HTML:

<div id="page_container">
<!--暗文密码输入框-->
<div class="input_block" id="psw_invisible">
<img  src="/static/imghwm/default1.png"  data-src="visible.png"  class="lazy"  id="visible" οnclick="showPsw()"  alt="How to hide and display passwords in html" >
<input type="password" id="input_invisible" placeholder="Password"/>
</div>
<!--明文密码输入框-->
<div class="input_block" id="psw_visible" style="display: none;">
<img  src="/static/imghwm/default1.png"  data-src="invisible.png"  class="lazy"  id="invisible" οnclick="hidePsw()"  alt="How to hide and display passwords in html" >
<input type="text" id="input_visible" placeholder="Password"/>
</div>
 
<button οnclick="">Login</button>
</div>

Then use JS to implement Alternate operation of click events: the password is hidden at first, click the small eye icon at the back to display the password, that is, the password input is hidden and the text input is displayed. At the same time, pay attention to passing the password value to the text, and vice versa. Here’s a lesson:

JS:

<script type="text/javascript">
// 这里使用最原始的js语法实现,可对应换成jquery语法进行逻辑控制
var visible=document.getElementById(&#39;psw_visible&#39;);//text block
var invisible=document.getElementById(&#39;psw_invisible&#39;);//password block
var inputVisible = document.getElementById(&#39;input_visible&#39;);
var inputInVisible = document.getElementById(&#39;input_invisible&#39;);
    //隐藏text block,显示password block
function showPsw(){
var val = inputInVisible.value;//将password的值传给text
inputVisible.value = val;
invisible.style.display = "none";  
visible.style.display = "";  
}
    //隐藏password,显示text  
    function hidePsw(){
   var val=inputVisible.value;//将text的值传给password  
inputInVisible.value = val; 
invisible.style.display = "";  
    visible.style.display = "none";  
}
</script>

The updated code is as follows:

HTML:

<div id="page_container">
<!--密码输入框-->
<div class="input_block">
<img  src="/static/imghwm/default1.png"  data-src="visible.png"  class="lazy"  id="demo_img" οnclick="hideShowPsw()"  alt="How to hide and display passwords in html" >
<input type="password" id="demo_input" placeholder="Password"/>
</div>
 
<button οnclick="">Login</button>
</div>

JS:

<script type="text/javascript">
// 这里使用最原始的js语法实现,可对应换成jquery语法进行逻辑控制
var demoImg = document.getElementById("demo_img");
var demoInput = document.getElementById("demo_input");
    //隐藏text block,显示password block
function hideShowPsw(){
if (demoInput.type == "password") {
demoInput.type = "text";
demo_img.src = "invisible.png";
}else {
demoInput.type = "password";
demo_img.src = "visible.png";
}
}
</script>

Demo It was originally free, but the system may have increased it to 10 points as more people signed up. Here is a github demo download address. If you don’t have points, you can download it for free from here:

https://github.com /jiangxh1992/HTML5InputDemo

【Recommended learning: html video tutorial

The above is the detailed content of How to hide and display passwords in html. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment