JS Event Handlers
.addEventListener() method attaches an event handler to a specific event on an event target. The advantage of this is that you can add many events to the event target without overwriting existing events. Two arguments are passed to this method: an event name as a string, and the event handler function. Here is the syntax:
We can tell our code to listen for an event to fire using the
.addEventListener() method. To tell the code to stop listening for that event to fire, we can use the
.removeEventListener() method. This method takes the same two arguments that were passed to
.addEventListener(), the event name as a string and the event handler function. See their similarities in syntax:
Event handler functions are passed an argument called an event object, which holds information about the event that was fired.
Event objects store information about the event target, the event type, and associated listeners in properties and methods. For example, if we wanted to know which key was pressed, the event object would store that information.
Keyboard events describe a user interaction with the keyboard. Each event describes a separate interaction with a key on the keyboard by the user, which are then stored with the
keydownevents are fired when the key is first pressed.
keyupevents are fired when the key is released.
keypressevents are fired when the user presses a key that produces a character value (aka is not a modifier key such as CapsLock).
JS Event Handlers
JS Event Handlers
The example code block shows how to register a function as an event handler. The property name for event handlers starts with ‘on’ with the event appended afterwards. Examples:
A mouse event fires when a user interacts with the mouse, either by clicking or moving the mouse device.
clickevents are fired when the user presses and releases a mouse button on an element.
mouseoutevents are fired when the mouse leaves an element.
mouseoverevents are fired when the mouse enters an element’s content.
mousedownevents are fired when the user presses a mouse button.
mouseupevents are fired when the user releases the mouse button.
- 1When you refresh your email inbox, double tap on a post, or scroll through a newsfeed — something cool happens in your browser. These actions are known as events! Events on the web are us…
- 2After a specific event fires on a specific element in the document object model (or DOM), an event handler function can be created to run as a response. In this lesson, we will learn about _even…
- 4It’s best practice to create named event handler functions, instead of anonymous functions. Your code will remain organized and reusable this way, even if your code gets complex. Check out the synt…
- 5The .removeEventListener() method is used to reverse the .addEventListener() method. This method stops the code from “listening” for an event to fire when it no longer needs to. .removeEventListene…
- 7Beyond the click event, there are all types of DOM events that can fire in a browser! It’s important to know most events in the DOM take place without being noticed because there are no event han…
- 8When you click down or move the mouse device mouse events fire. Outside of the click event, there are other events that can be fired when using a mouse. The mousedown event is fired when the use…
- 9Another popular type of event is the keyboard event! keyboard events are triggered by user interaction with keyboard keys in the browser. The keydown event is fired while a user presses a key do…