MDN self-hosted library · e01.org
HTML DOM API / notebook

HTML DOM API

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<p>Please provide the information below. Items marked with "*" are required.</p>
<p>
  <label for="userName" required>Your name:</label>
  <input type="text" id="userName" /> (*)
</p>
<p>
  <label for="userEmail">Email:</label>
  <input type="email" id="userEmail" />
</p>
<input type="button" value="Send" id="sendButton" />
js
const nameField = document.getElementById("userName");
const sendButton = document.getElementById("sendButton");

sendButton.disabled = true;
// [note: this is disabled since it causes this article to always load with this example focused and scrolled into view]
// nameField.focus();

nameField.addEventListener("input", (event) => {
  const elem = event.target;
  const valid = elem.value.length !== 0;

  if (valid && sendButton.disabled) {
    sendButton.disabled = false;
  } else if (!valid && !sendButton.disabled) {
    sendButton.disabled = true;
  }
});