Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <form id="loginForm" novalidate>
  <div>
    <label for="username">Username</label><br />
    <input type="text" id="username" name="username" />
  </div>
  <div>
    <label for="password">Password</label><br />
    <input type="password" id="password" name="password" />
  </div>
  <button type="submit">Log In</button>
</form>

<script>
  /**
   * Map of validation error keys to their spoken messages.
   * @type {{ [key: string]: string }}
   */
  const errorMessages = {
    usernameEmpty: "The Username field cannot be empty",
    passwordEmpty: "The Password field cannot be empty",
    // add more keys/messages here as needed...
  };
  /**
   * Speaks a given message using the Web Speech API.
   *
   * @param {string} message - The text to speak.
   * @param {string} [lang='en-US'] - The BCP-47 language code.
   */
  function speakError(message, lang = "en-US") {
    const utterance = new SpeechSynthesisUtterance(message);
    utterance.lang = lang;
    speechSynthesis.speak(utterance);
  }
  /**
   * Validates that the given input is not empty, styles it, and speaks an error if it is.
   *
   * @param {HTMLInputElement} element - The input element to validate.
   * @param {string} messageKey - The key identifying which error message to speak.
   */
  function validateNotEmpty(element, messageKey) {
    if (element.value.trim().length === 0) {
      element.style.border = "1px solid red";
      speakError(errorMessages[messageKey]);
    } else {
      element.style.border = "1px solid black";
    }
  }
  /**
   * @typedef {Object} FieldConfig
   * @property {string} id - The DOM id of the input field.
   * @property {string} messageKey - The key in errorMessages to use.
   */
  /** @type {FieldConfig[]} */
  const fields = [{
      id: "username",
      messageKey: "usernameEmpty"
    },
    {
      id: "password",
      messageKey: "passwordEmpty"
    },
  ];
  // Wire up blur listeners once DOM is ready
  fields.forEach(({
    id,
    messageKey
  }) => {
    const el = /** @type {HTMLInputElement|null} */ (document.getElementById(id));
    if (!el) return;
    el.addEventListener("blur", () => validateNotEmpty(el, messageKey));
  });
  // Prevent form submission for demo purposes
  const form = document.getElementById("loginForm");
  if (form) {
    form.addEventListener("submit", (e) => e.preventDefault());
  }
</script>
              
            
!

CSS

              
                form {
  max-width: 400px;
  margin: 2rem auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
label {
  font-weight: bold;
}
input {
  padding: 0.5rem;
  font-size: 1rem;
}

              
            
!

JS

              
                
              
            
!
999px

Console