An event notifies code with something "interesting" changes that might affect code execution or program flow. Some examples of events include:
See a full list of Event references on MDN Docks.
In order to capture events, we need use the addEventListener()
method.
The addEventListener()
method listens for a specific event and if the event is sensed, it executes a function. Let's take a look at the syntax:
target.addEventListener(type, listener);
target.addEventListener(type, listener, options);
target
: DOM reference to an HTML element. type
: A string that represents an event type. listener
: This is the function that fires if the event is captured. options
: This is an optional parameter that allows customization of specific characteristics of the event listener. Review all of the parameters on the addEventListener()
MDN Docs. The listener can also be combined with arrow function to make implementation more concise!
target.addEventListner(type, (a)=>{
//add function code here!
}