Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to create Animated No Chill Login form using HTML CSS and JS

    22 May 2025

    How to make Social media icons hover effect using HTML & CSS

    14 May 2025

    How to make Awesome Radar Animation using HTML & CSS

    8 May 2025
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - JavaScript - How to create Hide/Show Password using JavaScript
    JavaScript

    How to create Hide/Show Password using JavaScript

    Coding StellaBy Coding Stella17 April 2025Updated:17 April 2025No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    Let’s create a Hide/Show Password feature using HTML, CSS, and JavaScript. This simple and useful project lets users toggle the visibility of their password input, making forms more user-friendly.

    We’ll use:

    • HTML to create the password input field and toggle button.
    • CSS to style the form and add a clean look.
    • JavaScript to switch between showing and hiding the password when the button is clicked.

    Whether you’re building a login form or a sign-up page, this feature is a great way to improve usability. Let’s get started and add this helpful function to your web projects! 🔐👁️

    HTML :

    This HTML creates a simple password input layout. It includes a text field for entering a password and a button to show or hide it. It links to a CSS file (style.css) for styling and a JavaScript file (script.js) to handle the show/hide functionality. The structure is clean and centered on the page.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Show/Hide Password</title>
      <link rel="stylesheet" href="style.css">
    </head>
    <body>
    
      <div class="password-container">
        <input type="password" id="password" placeholder="Enter password">
        <button id="togglePassword" class="show-password">Show password</button>
      </div>
    
      <script src="script.js"></script>
    </body>
    </html>

    CSS :

    This CSS styles a password input section. It removes default spacing, centers everything on a dark background, and styles the password box with padding and rounded corners. The input field has a dark theme with white text, and the button is styled in blue, changing to red when hiding the password. There’s also a hover effect for the button to darken its color slightly.

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: sans-serif;
      }
      
      body {
        background-color: #252432;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
      }
      
      .password-container {
        display: flex;
        gap: 10px;
        align-items: center;
        background: #1e1e2f;
        padding: 10px;
        border-radius: 8px;
      }
      
      input[type="password"],
      input[type="text"] {
        padding: 8px;
        border: 1px solid #444;
        border-radius: 4px;
        background: #2c2c3c;
        color: white;
        outline: none;
      }
      
      button {
        padding: 8px 12px;
        border: none;
        width: 120px;
        background-color: #007bff;
        color: white;
        border-radius: 4px;
        cursor: pointer;
      }
      
      button:hover {
        background-color: #0056cc;
      }
      
      button.hide-password {
        background-color: #dc3545; /* red */
      }

    JavaScript:

    This code allows users to show or hide their password. When the button (togglePasswordButton) is clicked, it checks if the password input is hidden (type === "password"). If it is, the code changes the type to "text" to show the password and updates the button’s text and class. If the password is already visible, it hides it again and updates the button accordingly.

    const togglePasswordButton = document.getElementById("togglePassword");
    const passwordInput = document.getElementById("password");
    
    togglePasswordButton.addEventListener("click", function () {
      if (passwordInput.type === "password") {
        passwordInput.type = "text";
        togglePasswordButton.textContent = "Hide password";
        togglePasswordButton.classList.remove("show-password");
        togglePasswordButton.classList.add("hide-password");
      } else {
        passwordInput.type = "password";
        togglePasswordButton.textContent = "Show password";
        togglePasswordButton.classList.remove("hide-password");
        togglePasswordButton.classList.add("show-password");
      }
    });
    

    In conclusion, the Hide/Show Password feature using JavaScript is a quick and easy way to enhance your web forms. It improves the user experience by allowing them to view or hide their input, making password entry more convenient and error-free.

    If your project has problems, don’t worry. Just click to download the source code and face your coding challenges with excitement. Have fun coding!

    hide and show password JavaScript password
    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleHow to make Magic Social Share Menu using HTML CSS and JS
    Next Article How to make Cool Loading Animation using HTML & CSS
    Coding Stella
    • Website

    Related Posts

    JavaScript

    How to create Animated No Chill Login form using HTML CSS and JS

    22 May 2025
    JavaScript

    How to create Cross Road Game using HTML CSS and JS

    2 May 2025
    JavaScript

    How to create 3D Animated Bee website using HTML CSS and JS

    28 April 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202419K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202416K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202414K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202412K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    How to make Scroll-driven Animations using HTML & CSS

    9 September 2024

    How to Make Login Form with Captcha in HTML, CSS & JavaScript

    11 January 2024

    How to make Custom File Upload Button in HTML CSS & JavaScript

    25 February 2024

    How to make Step Indicator – Timeline using HTML CSS & JavaScript

    7 October 2024
    Latest Post

    How to create Animated No Chill Login form using HTML CSS and JS

    22 May 2025

    How to make Social media icons hover effect using HTML & CSS

    14 May 2025

    How to make Awesome Radar Animation using HTML & CSS

    8 May 2025

    How to create Cross Road Game using HTML CSS and JS

    2 May 2025
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2025 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood