Events in JavaScript
Listening for user interaction and form submission.
const form = document.querySelector("form");
form.addEventListener("submit", (event) => {
event.preventDefault();
const data = new FormData(form);
console.log(Object.fromEntries(data));
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
form.reset();
}
}, { once: true });
How it works
addEventListenerbinds a handler to an event.preventDefaultstops the browser's default action.FormDatareads the fields;{ once: true }auto-removes.
Keywords and builtins used here
Objectconstdocumentif
The run, in numbers
- Lines
- 13
- Characters to type
- 307
- Tokens
- 87
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 52 seconds.
Step 1 of 3 in Events, step 4 of 13 in The browser.