<form-show-if> via esm.sh

Import the class, then register it. See the README for the API.

Select Email to reveal the dependent field. Focus stays on the select. Without JavaScript or if the CDN import fails, the authored email field remains available. Check the console and import URL if enhancement fails; entered values remain in the native fields.

Manual registration

<form>
  <label for="contact-method">Preferred contact method</label>
  <select id="contact-method" name="contact-method">
    <option value="">Select one</option>
    <option value="email">Email</option>
    <option value="phone">Phone</option>
  </select>
  <form-show-if conditions="contact-method=email">
    <label for="email">Email address</label>
    <input type="email" id="email" name="email" />
  </form-show-if>
</form>

<script type="module">
  import { FormShowIfElement } from
    'https://esm.sh/@aarongustafson/form-show-if@3.1.5/form-show-if.js?bundle&target=es2022';
  if (!customElements.get('form-show-if')) {
    customElements.define('form-show-if', FormShowIfElement);
  }
</script>