A Progressive Enhancement Pattern With Custom Elements

A password field can remain useful without JavaScript while an optional button makes its value visible. That is a small progressive enhancement pattern with custom elements: keep the essential control in HTML and add the convenience only after its behaviour exists.

The failure state is easy to explain. If the script never loads, the password stays masked and there is no inactive “Show password” button to puzzle over.

Start with a labelled password input

The wrapper contains a labelled native input:

<show-password>
  <label for="password">Password</label>
  <input
    id="password"
    type="password"
    name="password"
    autocomplete="current-password"
  >
</show-password>

<script type="module" src="/js/show-password.js"></script>

The example uses current-password for an existing password, as on a sign-in form. A registration or password-change form needs the appropriate autocomplete value instead.

No submission endpoint is included here. The component adds visibility behaviour to an input; the surrounding form still owns authentication and submission.

Give the wrapper a layout explicitly rather than relying on an unstyled custom tag:

show-password {
  display: block;
}

show-password button {
  margin-inline-start: 0.5rem;
}

Add the button after finding the input

Save this definition as /js/show-password.js:

class ShowPassword extends HTMLElement {
  #ready = false;

  connectedCallback() {
    if (this.#ready) return;

    const input = this.querySelector('input[type="password"]');
    if (!input) return;

    const button = document.createElement("button");
    button.type = "button";
    button.textContent = "Show password";

    button.addEventListener("click", () => {
      const reveal = input.type === "password";
      input.type = reveal ? "text" : "password";
      button.textContent = reveal ? "Hide password" : "Show password";
    });

    this.append(button);
    this.#ready = true;
  }
}

customElements.define("show-password", ShowPassword);

The input’s type property changes how the existing input presents its value. The component does not create a second field or copy the password elsewhere.

The button says what the next click will do. After revealing the value, “Hide password” becomes the available action. A native button also supplies the ordinary keyboard interaction, without recreating it on a clickable span.

Setting type="button" matters inside a form. This control changes visibility; it should not accidentally submit the user’s credentials.

Reconnecting should not add another control

The custom element connection callback can run more than once. Removing and reinserting this wrapper should not create a row of identical buttons.

The private #ready flag is set only after setup succeeds. A reconnect with the same input keeps the existing button and listener.

That guard has a deliberately small scope. It does not detect a replaced input or resynchronise the button if other code changes the field’s type. Those behaviours need explicit support if the surrounding application requires them.

For this static wrapper, the listener belongs to a button inside the component. There is no document-wide event subscription or timer to manage. Adding one later would require reconsidering cleanup when the component disconnects.

Make the loading assumption visible

The example uses a non-async module script. Its normal timing means the initial HTML is parsed before the definition runs.

That matters because setup searches for a child input. Defining the element early in a blocking head script can connect the wrapper before its children have been parsed.

Content inserted later should arrive as a complete wrapper with its input. Appending an empty wrapper and then adding the input after its callback has run needs another initialisation strategy.

An initially hidden button is another reasonable choice

The button can also live in the HTML with a hidden attribute. A script would attach its listener before revealing it.

I would keep the button in HTML when translated copy or a server-side template needs to own its label. Generating this tiny control is convenient when the component owns both the behaviour and those two strings.

Either choice should preserve the same sequence: an operable password field first, then an optional visibility control. Check keyboard operation and label changes in the actual form, including any visibility button the browser supplies itself.

The broader HTML enhancement approach works because that baseline remains useful. Here, the component’s responsibility ends at showing or hiding one existing value.