Forms and Javascript

Event object

You cannot pass arguments to event handlers but the browser creates and event object for you and makes it optionally available as an argument for your event handler. You can name this anything you want (usually e, evt or event).

const clickHandler = function(event) {
console.log(event);
}

Handy properties of the event object:


HTMLFormElement.elements

The <form> object provides easy access to its form controls (i.e. text fields, radio buttons, etc) with the elements property. Given the following form (labels are omitted for clarity):

<form action="some-url" method="post">
<input type="text" name="username" id="username">
<input type="password" name="password" id="password">
<input type="submit" value="Submit">
</form>

And the following Javascript:

const form = document.querySelector('form');