Loading Depth Study
Preparing your workspace
Events represent things that happen in the browser, and event listeners allow JavaScript to react to those actions.
Web applications depend heavily on events. Common events include click, input, change, submit, keydown, mouseover, and load. addEventListener is the standard way to register a handler for an event. Event objects provide information about what happened and which element triggered the event. Forms often require preventing default browser behavior before custom submission logic runs. Event delegation can be useful when many child elements share similar behavior.
A quiz page can listen for a submit event, check the selected answer, and immediately show whether the answer is correct.
const form = document.querySelector('#loginForm');
form.addEventListener('submit', event => {
event.preventDefault();
console.log('Form submitted');
});The browser does not perform the default form submission, and JavaScript prints 'Form submitted' instead.