DEV Community

Hilton Meyer
Hilton Meyer

Posted on • Originally published at hiltonmeyer.com on

2

Toggle Multiple Fields

Today's project for the Vanilla JS Academy was creating a checkbox that would either show the text of the password or hide it behind a password type input. This was very similar to yesterdays in that we were given two password fields and had to toggle between seeing the password or not. In this case there were two password fields like you might have on a change password field.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Multi Field Toggle</title>
  </head>
  <body>
    <h2>Password Toggle Visibility</h2>

    <form>
      <div>
        <label for="current-password">Current Password <input type="password" name="current-password" id="current-password" /></label>        
      </div>

      <div>
        <label for="new-password">New Password <input type="password" name="new-password" id="new-password" /></label>        
      </div>

      <div>
        <label for="show-passwords">
          <input type="checkbox" name="show-passwords" id="show-passwords" />
          Show passwords
        </label>
      </div>

      <p>
        <button type="submit">Change Password</button>
      </p>
    </form>

    <script src="script.js"></script>
  </body>
</html>

The solution is very much based on what I did yesterday and tried to re-use the same functions.

  1. Select the elements of the checkbox
  2. Get an array of the password fields
  3. Check when the checkbox was clicked
  4. Toggle the password fields using a loop
// Elements
const showPasswords = document.querySelector("#show-passwords");
const passwordFields = Array.prototype.slice.call(document.querySelectorAll("[type='password']"));

// Toggle Password Field
const togglePasswordFields = () => {
  passwordFields.forEach((password) => {
    password.type = showPasswords.checked ? "text" : "password";
  });  
};

// If page is refreshed and the checkbox is already refreshed
// change the fields to text in anyway
if (showPasswords.checked) {
  passwordFields.forEach((password) => {
    password.type = "text";
  });
}

// Listen for checkbox click
showPasswords.addEventListener("click", togglePasswordFields, false);

I ran into a sticky situation that actually came up in the thread on Slack where someone mentions that when refreshing the page it would still keep the checkbox checked but the passwords would disappear but also loose the functionality of changing the input type. The solution was to add a check to make sure that if on load the checkbox was checked then to change the fields to text on the first run.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay